0
\$\begingroup\$

What is the best way to reference multiple Canvas Images within C# script attached to my player in Unity?

Currently I have something similar too:

public Image image;

//Finds the Image Object being referenced.
image = GameObject.Find ("Canvas/Image").GetComponent<Image> ();
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You may find this answer useful. It recommends wiring up this kind of relationship in the Inspector, so that changes to your scene/prefab hierarchy don't accidentally cause "Find"-based methods to find the wrong object, or none at all. \$\endgroup\$ Commented Feb 15, 2016 at 14:52
  • 1
    \$\begingroup\$ Personally when referencing large numbers of objects I like to create a separate class e.g. ExampleClassReferences for my script ExampleClass which contains all the references as public objects and mark it as [System.Serializable]. I then have a public instance of ExampleClassReferences in ExampleClass so I can then assign them all in the inspector and minimize the class to stop it taking up loads of space. (Ofc if you are instantiating the object with ExampleClass some of the references may not carry over, this also may not be the best practice not sure) \$\endgroup\$ Commented Feb 16, 2016 at 14:49

1 Answer 1

1
\$\begingroup\$

Well, as far as I understand your question, you can assign whole array on Image type.

public Image[] images;

//Finds the Image Object being referenced.
images = FindObjectOfType<Canvas> ().transform.GetComponentsInChildren<Image> ();
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.