- Home /
 
               Question by 
               Wavenimbus42 · Aug 12, 2021 at 09:32 PM · 
                c#scripting problemprefab  
              
 
              Changing a Prefab's Script's Variables
I have a card Prefab with a couple of variables: public string cardName and public byte index. Other information is generated based on those values for an instance of said Prefab. In a different controller, I have a function that's trying to create unique cards from that Prefab:
 public GameObject cardPrefab
 void makeCard(byte index)
 {
     GameObject newCard = Instantiate(cardPrefab ...)
     newCard.name = "Testing";
     PrefabScript cardScript = newCard.GetComponent<PrefabScript>();
     cardScript.cardName = "Useful_Item";
     cardScript.index = 30;
 }
From simple testing, this works up until I attempt to access the PrefabScript (which is a Component of the Prefab), which returns NULL somehow. Why?
               Comment
              
 
               
              Answer by reytor9010 · Aug 13, 2021 at 12:55 AM
If the prefab has the PrefabScript component, that should work. Could you check in the hierarchy if the instantiated prefab have this component?
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                