instance of prefab can't be found outside of Start() function

I am a beginner at coding. To clarify I am creating for instance a clone of BetButton.
I only want the clone to be SetActive if Allowed.Contains has (“Bet”) in it.
My Betbutton and slider are both working and initially not Active on screen, which I expect, but I can’t get a proper reference to clone1/ clone2 prefabs outside of Start() function to call the SetActive(True) fucntion on the Original prefab.

I tried it with tags on the Prefab.
also tried with declaring Public GameObject clone1; and by then selecting the BetSize prefab and putting it into GameObject clone1 slot in the inspector so that it actualy refers to the Prefab, but no luck.
Out of desperation requesting help :slight_smile:

I constantly get the message Object reference not set to an instance of an object.

public class UIManagerScript : MonoBehaviour {
	//public Animator Bet;
	//public Animator settingsButton;
	public GameObject BetButton;
	//public Transform betButton1;
	//public Resolution [] resolutions;
	public Transform menupanel;
	public GameObject BetSize;
	public Slider myslider;
	//public GameObject clone2;
    public void Start() {
        GameObject clone1 = (GameObject)Instantiate (BetButton, new Vector3 (200, 150, 0), Quaternion.identity);
        		clone1.transform.SetParent (GameObject.FindObjectOfType<Canvas> ().gameObject.transform, true);
        		
        		GameObject clone2 = (GameObject)Instantiate (BetSize, new Vector3 (200, 150, 0), Quaternion.identity);
        		clone2.transform.SetParent (GameObject.FindObjectOfType<Canvas> ().gameObject.transform, true);
        
        		clone1.SetActive (false);
        		clone2.SetActive (false);
        		var foundScript1 = gameObject.GetComponentInChildren<UpdateValue> ();
        		myslider = clone2.GetComponent<Slider> ();
    }
        	public void BettingRound (NetworkPlayer actors, int Xpos, int Ypos,  List<string> AllowedActions) {
        
        				if (AllowedActions.Contains ("Bet")) {
        						
        			GameObject clone1 = GameObject.FindGameObjectWithTag("BetButton"); 
        		GameObject clone2 = GameObject.FindGameObjectWithTag("BetSize"); 
        			clone1.SetActive (true);
        			clone2.SetActive (true);

:slight_smile:

You are declaring clone1 in the Start’s Scope. I usually declare local variables to a method starting with “_”- to make it clear.

The solution to your problem is to a declare the variables before the Start() method;

 public class UIManagerScript : MonoBehaviour {

     public GameObject BetButton;
     public Transform menupanel;
     public GameObject BetSize;
     public Slider myslider;
     GameObject clone1;
     GameObject clone2;

     public void Start() {
         clone1 = (GameObject)Instantiate (BetButton, new Vector3 (200, 150, 0), Quaternion.identity);
         clone1.transform.SetParent (GameObject.FindObjectOfType<Canvas> ().gameObject.transform, true);
         clone2 = (GameObject)Instantiate (BetSize, new Vector3 (200, 150, 0), Quaternion.identity);
         clone2.transform.SetParent (GameObject.FindObjectOfType<Canvas> ().gameObject.transform, true);
         clone1.SetActive (false);
         clone2.SetActive (false);
         var foundScript1 = gameObject.GetComponentInChildren<UpdateValue> ();
         myslider = clone2.GetComponent<Slider> ();
     }