- Home /
Setting the variable on a GameObject to be instantiated (not yet existent) cs and js
I'm trying to set an exposed variable on a gameobject that has not yet been instantiated.
var s:GameObject=GameObject.Find("instantiateMe"); var instances:GameObject; instances = Instantiate(s,pos,rot); instances.GetComponent(ScriptName).id=id;
Using the usual method above, I get this fatal error: "BCE0005 Unknown identifier: ScriptName"
instantiateMe
(the object to be instantiated) has a script named ScriptName
in it, with the global variable id
...
Using this method, I get "BCE00018: The name 'ScriptName' does not denote a valid type ('not found')."
var s:GameObject=GameObject.Find("instantiateMe"); var instances:GameObject; instances = Instantiate(s,pos,rot); var script:ScriptName=instances.GetComponent(ScriptName); script.id=id;
ScriptName.cs is a cs file! But, I'm using js on the other ones as I am more familiar with that.
Answer by StephanK · Dec 28, 2010 at 11:49 AM
It looks like you haven't defined the variable ScriptName anywhere in your code. GetComponent takes a string as a parameter not a type so you would have to write:
instances.GetComponent("ScriptName").id = id;
No, that doesn't work because then Unity thinks we are calling Unity Components. Using your snippet, I get this other fatal error: "BCE0019: id is not a member of UnityEngine.Component"
It's javascript? Try this: var script : ScriptName = instances.GetComponent("ScriptName"); script.id = id;
tried that as well - "The name 'ScriptName' does not denote a valid type ('not found'). ..." - checked ScriptName, and it's clearly in instantiate$$anonymous$$e
if ScriptName is not a type you either don't have a script that is named ScriptName.js or the compiler doesn't know about it. (Could be the case if it's in Editor or Plugins folder)
Answer by Loius · Dec 28, 2010 at 04:00 PM
Try creating a folder called "plugins" or "Plugins" (not sure if capitalization is important) and putting your .cs files in there. THat'll cause them to be compiled earlier than most other scripts, and your .js files will know about their variables.