Using Data with a Button?

Hello,

(What I am doing)
What I am doing is I have a button prefab in a scrollable panel. The button and its text is loaded through WWW, as well as instantiating more prefabs for each button needed.

When a user clicks on one, it should use an IEnumerator to load the story associated with that title and then open a new panel with the story loaded. So what I was thinking was attaching a C# script to the panel holding the button prefabs and when a button is clicked, it will load the story associated with that button ID via WWW.

(The Problem)
My question is how the buttons know which story to load? My idea was to attach and use the prefab’s C# script to hold the story ID and when it’s clicked: the main script can ask that C# script what ID it was. How do I do that though, and also change the script’s ID for every new button i instantiate?

1 Answer

1

@Rotavele, you have got the right idea to deal with your requirements and here is my help to you for your stuff. Ask me further if you will have any regards with this.

 /// Instantiates the button prefab for each story.
    	/// <param name="storyId">Story identifier.</param>
    	void InstantiateButtonPrefab(int storyId)
    	{
    		GameObject go = Instantiate(buttonPrefab) as GameObject;
    		go.GetComponent<Button>().onClick.AddListener(()=>{
    			//when this button clicked, it will load the story wrt storyId
    			LoadStory(storyId);
    		});
            //do your stuff
    	}
    
    	/// Loads the story.
    	/// <param name="storyId">Story identifier.</param>
    	void LoadStory(int storyId)
    	{
    		//do your stuff
    	}

i have to check if the player is still in the collider of many objects, i cannot check if every one is active or not. i'm making a system that detect if the player is in a light zone( a collider on a certain layer) some of the object that contain the collider turn off some time then turn on... i find a way to achieve that with the overlap sphere but it cause me an other problem...