Reference multiple componenets from other classes

Started by
2 comments, last by frob 5 years, 8 months ago

I am developing a progression system game in Unity3D and in a scene I have multiple GameObjects that have the UI button component. I am trying to find a way to store reference to all of these UI button components in one class and than access those buttons from other classes. I have thought about setting up a class with public Button variables and than adding this class as a component to GameObject however this does not feel optimum. Is there a way to create a reference to multiple UI buttons from once class and than access the referenced variables from other classes? 

Summary can be seen below 

- scene in unity 3D with multiple UI buttons

- trying to find a way store UI buttons in one class

- access UI buttons from other classes that have been instantiated in scene 

Advertisement

public class Buttonid : MonoBehaviour {

	#region Singleton 

	private static Buttonid _instance;

	public static Buttonid Instance

	{


		get

		{

			if (_instance == null) {

				GameObject go_Buttonid = GameObject.FindGameObjectWithTag ("Buttonid");
				go_Buttonid.AddComponent<Buttonid> ();
			}

			return _instance;
		}


	}

	#endregion


	public Button[] button ; 


	void Awake ()
	{
		_instance = this;


	}

	public void buttonslist ()

	{
		


	}

Progress so far. 

18 hours ago, davejones said:

I am trying to find a way to store reference to all of these UI button components in one class and than access those buttons from other classes.

Implementation details are important. There are many ways to do it, but they depend on what you need in the end.

You might be able to create the buttons dynamically in code, storing each one in a collection as you create it. Maybe they're cloned as prefabs or maybe you build them dynamically (such as pie slices in a circle), track them as you create them.

You might leverage parenting. All the buttons parented directly under a specific object get collected at runtime. GetComponentsInChildren() and similar functions can help you find all the sub-objects with UI Button components.

Another option is to have each UI button have a callback in your own MonoBehaviour attached to each object. You'll need to set the target object and function for each one you create.

Another option is to have a collection in another object that you manually maintain, dragging links from each UI button into the inspector. 

Your use of a Singleton and tags can work but should be cleaned up. Singletons tend to be problematic on their own, I don't want to start a religious war on them, just search the Internet on the problems with Singletons. Rather than using FindGameObjectsWithTag() that searches the entire game world for objects, you are probably better off searching children only. That is more effort and isn't built in.

 

Once you have links to all of them in one convenient place, the collection can be passed around to other places as needed.

This topic is closed to new replies.

Advertisement