Home > database >  Select 2 buttons, show image and store value in unity/c#
Select 2 buttons, show image and store value in unity/c#

Time:11-30

I'm developing an app and what I wish to achieve is: When a button is clicked, the colour of an object changes, store that value, and when another button is clicked, another image appears. Then, I need to store this values and make the image show in a different screen. How can I achieve this?

Thank you

CodePudding user response:

Try this tutorial; https://learn.unity.com/tutorial/ui-components#

Cache your GameObjects locally at Awake() or Start() to keep references to them for gameObject.SetActive(true/false) and to sample the gameObject.GetComponentInChildren().color.

Another tip is to use Prefabs for object you will use a lot or want control in how they are built and the components they contain. This is very easy, create an empty gameObject in your Hierarchy named how you like (myImagePrefab etc), create a Prefabs folder in your Project Assets/ the same you would for Assets/Scripts or Assets/Scenes, and drag your object in to that. You can then delete the object in the hierarchy and use the Prefab to control which components are on it (Image / Script / RigidBody etc). To use this cache a version in your script that will control that part of your scene;

[SerializeField] private GameObject _myPrefab;

This will appear in the inspector and you can drag your Prefab here. Use Instantiate to create as many versions as you need and set the awake or get colour, or anything really. These are some basics to get started.

  • Related