- Home /
Accessing variable defined in a Script attached to Prefab
I have a public variable defined as public var ElementJson:SimpleJSON.JSONClass;
on a script named ElementInfo
which is attached to a prefab Element.I am using Simple JSON Parser for storing element information in JSON
.Suppose there are 10 instance of Element i.e Element1-Element10. And ElementInfo
is attached to each of the instance. Now my question is how to access ElementJson
of each instance in a script that is attached to a empty GameObject
.This script is used for accessing overall application information i.e one can say a controller script which will handle entire application data and pass it to other components.!
I tried doing this
var elementInfo:ElementInfo;
function Start ()
{
elementInfo=FindObjectOfType(typeof(ElementInfo));
}
function Update ()
{
print(elementInfo.Element.ToString());
}
But it shows
NullReferenceException: Object reference not set to an instance of an object
TestScriptForJson.Update () (at Assets/ReactionScreenScript/TestScriptForJson.js:30)
can you point out what is the problem. I have been using this method successfully for accessing variables of other scripts. And just one more question for understanding purpose. Since there are multiple instances of prefab so when accessing the variable how will it come to know which element it is accessing or it will take information from all the instances.
That's not how your properly get components from other game objects. FindObjectOfType of used to find game objects, not to get a component. That's what GetComponent is there for. Get the component, make sure it's not null and then use it to fetch any variable you want.
As per your query on whether FindObjectOfType gets the first or all instances, from this link: "Returns the first active loaded object of Type type. Please note that this function is very slow. It is not recommended to use this function every frame. In most cases you can use the singleton pattern ins$$anonymous$$d."
Again, you're still confusing between 'finding an object' and 'getting a component/script from an object' - FindObjectOfType will search the whole scene and once it finds the first active object of the type you specify it will return it to you. While if you had a game object and wanna get a component from it, you should do gameObject.GetComponent
I strongly suggest you try to avoid all those FindObject variations. In some cases you have no choice, and this is a good option, but you should familiarize yourself with other mechanisms for obtaining a reference to an object. I have developed two full games now, and never used FindObjectOfType or its array returning brother.
Answer by vexe · Sep 11, 2013 at 06:10 AM
You just need a call to GetComponent from the script you're trying to access the component SimpleJSON from. More detailed information on getting components can be found here and here.
Answer by sanks007 · Sep 11, 2013 at 06:37 AM
Thanks both of you..! this is what I did and it worked..
var Element:SimpleJSON.JSONClass;
var elementInfo:ElementInfo;
function Update ()
{
var gameobject : GameObject;
for(gameobject in testscript.elementarrays)
{
elementInfo=gameobject.GetComponent("ElementInfo");
Element=elementInfo.Element;
print(Element.ToString());
}
}
Since the instances of Element are created at runtime kept the code in the update method and now am able to access it. And thanks guys once again..! @vexe FindObjectOfType
i am calling this function in start method and not in update.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can one use of a static variable, change it globaly? 2 Answers
Showing GUI by pressing diffrent buttons? 1 Answer
How can I take a variable from another script and then apply it to a newly insantiated prefab? 2 Answers
Is there a way to set components in a prefab before it is Instantiated? 0 Answers