- Home /
Access a function of an instantiated prefab
I have an instance of a prefab. That prefab has a script attached. I want to access to a function of that script. How do I do that?
I instantiated the prefab as this: Instantiate(prefab,transform.position,Quaternion.identity);
Answer by Lo0NuhtiK · Jan 26, 2012 at 03:24 PM
 var clone : prefabType ; //Transform, Rigidbody, whatever it is that you have for ur prefab slot  eg prefab : Rigidbody ;
 clone = Instantiate(prefab, yourposition, yourrotation) ;
 var cloneScript : NameOfScript ;
 cloneScript = clone.gameObject.GetComponent(NameOfScript) as NameOfScript ;
 cloneScript.FunctionNameInItImTryingToDo() ;
The prefab has already attached the script as a component... they will still be linked with that code?
This code doesn't attach a script to anything, it gets a script that is already attached to the instantiated game object ; so yes, it links to the attached script.
 var clone --> makes the instantiated prefab an accessible variable.
 var cloneScript --> is a variable to hold a certain script, and we give it its value by accessing 'clone's gameObject and getting a component (the script) from it.
Your answer
 
 
             Follow this Question
Related Questions
How to instantiate a prefab after a certain action happened? 2 Answers
Accessing prefab data before instantiation breaks prefab? 3 Answers
Calling a function from a PreFab 1 Answer
Referencing in a prefab 1 Answer
Get unity to recognize prefab in C# 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                