- Home /
 
 
               Question by 
               adshead · Mar 08, 2012 at 04:43 PM · 
                instantiatepropertiesinitialise  
              
 
              Instantiate a Prefab *and* initialise some of its properties in c#?
Hi all,
I have a main game script in c# that is successfully instantiating prefabs. On my prefab I have just attached a script exposing three public properties. I can see these properties in the property inspector.
Is it possible to set those properties through c# code at the same time as I instantiate the prefab?
               Comment
              
 
               
              Answer by Jacob-Aldridge · Mar 08, 2012 at 04:50 PM
Yes, you should be able to call it like this:
 GameObject go = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
 SomeComponent comp = go.GetComponent<SomeComponent>();
 comp.property1 = "blah";
 comp.property2 = 2;
 ect...
 
              Your answer
 
             Follow this Question
Related Questions
Instantiate to temp var or directly adding to list? 1 Answer
Why Instantianting material does not copy all properties 1 Answer
rate of spawn based on speed of object 1 Answer
When is Awake called on an inactive gameobject 3 Answers
Instantiate a prefab and initialise some of its properties? 1 Answer