0

I am trying to adapt Unity DraggingAndDroppingMultiple project so that when you click a button it randomly selects an image from an array of images and updates the texture on a cube. The aim is to place these photo objects in AR.

I am using Unity 2019.1.10f1 and am getting some errors.

I have the following in SpawnPhoto.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnPhoto : MonoBehaviour
{
    public GameObject photoPrefab;
    public static Sprite[] photoLibrary;
    public string photoName;


    int arrayIdx = Random.Range (0, photoLibrary.Length);
        Sprite photo = photoLibrary[arrayIdx];
        photoName = photo.name;


}

and in Placement WithMultipleDraggingDroppingController.cs



    void Awake() 
        {
            arRaycastManager = GetComponent<ARRaycastManager>();
            dismissButton.onClick.AddListener(Dismiss);

            photoID = GetComponent<SpawnPhoto>().photoName;


            if (redButton != null && greenButton != null && blueButton != null) 
            {
                redButton.onClick.AddListener(() => ChangePrefabSelection("ARRed"));
                greenButton.onClick.AddListener(() => ChangePrefabSelection("ARGreen"));
                blueButton.onClick.AddListener(() => ChangePrefabSelection("ARBlue"));
                yellowButton.onClick.AddListener(() => SelectRandomPhoto($"{photoID}"));
            }
        }

        private void SelectRandomPhoto(string name)
        {
            GameObject loadedGameObject = Resources.Load<SpawnPhoto>($"Prefabs/{name}");

            if (loadedGameObject != null)
            {
                PlacedPrefab = loadedGameObject;
                Debug.Log($"Game object with name {name} was loaded");
            }
            else
            {
                Debug.Log($"Unable to find a game object with name {name}");
            }
        }
        private void ChangePrefabSelection(string name)
        {
            GameObject loadedGameObject = Resources.Load<GameObject>($"Prefabs/{name}");
            if(loadedGameObject != null)
            {
                PlacedPrefab = loadedGameObject;
                Debug.Log($"Game object with name {name} was loaded");
            }
            else 
    when saving the console shows an issue with photoName variable in the first block above.

saying that Invalid Token '=' in class, struct or interface member declaration.
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    Yes, you can't do this: `Sprite photo = photoLibrary[arrayIdx];` – Draco18s no longer trusts SE Jul 26 '19 at 17:55
  • 1
    Possible duplicate of [A field initializer cannot reference the non-static field, method, or property?](https://stackoverflow.com/questions/7400677/a-field-initializer-cannot-reference-the-non-static-field-method-or-property) – Ruzihm Jul 26 '19 at 18:02

1 Answers1

0

You are mixing static and non-static fields in your class. You cannot mix the two in a field declaration. Is there a reason you need the photoLibrary field to be static?

If you're looking to make this type of data a singlton, I would look into certain Unity3D singleton patterns: https://www.studica.com/blog/how-to-create-a-singleton-in-unity-3d

Otherwise, remove the static portion to: public Sprite[] photoLibrary;

EDIT:

Also move the retrieval of the photo name into a method:

public string GetRandomPhotoName()
{
    int arrayIdx = Random.Range (0, photoLibrary.Length);
    Sprite photo = photoLibrary[arrayIdx];
    return photo.name;
}

Then change the accessing line to: photoID = GetComponent<SpawnPhoto>().GetRandomPhotoName();