- Home /
How to access script variables attached to a prefab at runtime in Javascript?
I have a script that generates a grid of prefab tile objects at runtime. I have attached a script to the prefab that contains a single Vector3 to store the grid coordinates of each tile as it's instantiated. How do I access the script component of the prefab from the script instantiating them? I've tried a bunch of different things, but they don't work because a prefab isn't a class.
Thanks for any help.
I think the prefab to do something like script needs to be in scene so maybe you can take it to the scene then after the script does what you want to save it to your prefab and then you destroy the game object you have in scene.
Answer by DaveA · Mar 16, 2011 at 12:39 AM
If I understand correctly, you'd do it just like any other object. Once instantiated, you can access the component (script) then anything in it: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.htmland http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
The prefab isn't a class, it's an object, but you can get the component (script) off an object, thus stuff in that script.
If that doesn't do it for you, can you post some code?
Ah, that cleared it up, thank you!
basically I did this:
newHex = Instantiate(grassHex, Vector3(curX,curY,curZ), hexRotation); newHex.GetComponent(hexProperties).setCoords(new Vector3(j,i,k));
where hexProperties is the script component attached to the grassHex prefab.
Thanks again!
Answer by Pawan Asati · Jul 18, 2013 at 05:12 AM
< YourObjec type> GirlAnimation=(Resources.Load("GirlAnimation")) as BoneAnimation;
Your prefab should be keep in assts/Resources forlder then you can use it