Jump to content
  • Advertisement
davejones

C# Reference multiple componenets from other classes

Recommended Posts

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 

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!