- Home /
Accessing a script on a prefab after Instantiate
I know this has been asked before, but I think maybe either the answers I've found have not been for JavaScript or are for older versions of Unity because I keep getting errors that I'm not "denoting a proper type".
Basically I want to Instantiate a prefab, then call a function that is part of the script on that prefab to set some initial variables. my function is called Init
var newPrefab:GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;
var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);
initOnNewPrefab.Init(i,tempColor1, tempColor2);
this line
var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);
was adapted from another answer I found. I found a similar one that would have written it like this
cloneScript = newPrefab.gameObject.GetComponent(NameOfScript) as NameOfScript ; cloneScript.FunctionNameInItImTryingToDo() ; But I get the same error here. My guess is that I need to import some sort of library to access a variable that stands for a script name. At least that's how I read these two examples. But I don't what the library would be. The other thing I have tried is var newPrefab:GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;var cloneScript : NameOfScript ;
newPrefab.GetComponents(Init(i,tempColor1, tempColor2));
But this gives me "Unknown identifier:init" Again I feel like what I'm missing is importing in my script so it's usable.
I would instantiate the prefab from InitScript then have a script attached to the prefab that calls the Init function directly
// InitScript
// Instantiate your prefab from here
var tempColor1 ...
var tempColor2 ...
var newPrefab : GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;
// PrefabScript
// reference InitScript for tempColor vars and call Init function directly
private var initScript : InitScript;
function Awake()
{
// Set up the reference to InitScript
initScript = FindObjectOfType(typeof(InitScript));
}
function Start()
{
Init(i, initScript.tempColor1, initScript.tempColor2);
}
function Init(...)
{
...
}
Okay thanks. I see what I've been doing wrong. I've been looking at Unity sort of inside out, I think because of how the hierarchy window looks. I've been treating sprites as models ins$$anonymous$$d of as views, and the empty game objects at the top as views ins$$anonymous$$d of models/controllers. Now I see why I was so confused and things weren't working the way I was expecting.
Answer by Eric5h5 · Aug 01, 2014 at 04:10 AM
You have
var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);
But those are two different types. The type of the component must match the type of the variable. If the error says that Init is unknown, then you have no type called Init, simple as that. So you have to use the correct type.
Your answer makes sense to me, but what I don't get is what sort of variable I need to create to be able to access my function. If the component I'm trying to get is a script, should I be using a String that is equal to the name of the script? The examples I've found make it seem like there is some sort of Script class in Unity, but I can't seem to find how to access it.
The name of the script is the type. You would not use any strings.