- Home /
Can't retrieve information from GameObject
Good afternoon.
I'm currently having trouble trying to retrieve information from a gameObject. And maybe there's a workaround what I'm doing while using another more-db-like-approach, which would appreciate very much.
Trouble is, I have the following JS:
var Realname: String;
var description: String;
var price: float;
That JS is assigned to a prefab (gameObject).
Now I have another JS :
var currentObject : GameObject;
function Start () {
Debug.Log(currentObject.name);
Debug.Log(currentObject.description);
}
After I assign the prefab to currentObject in the inspector , my debug.log results are:
1) prefab name (not the Realname var)
2) NULL
Any ideas?
Thanks a bunch.
Answer by YoungDeveloper · Jul 18, 2014 at 04:16 PM
Hi, GameObject itself does not contain those values, GameObject contains scripts and scripts contain it. Use getcomponent to access others scripts. I explained it ver clearly in this answer: http://answers.unity3d.com/questions/730088/transform-var-between-scripts.html There's an example in c# and unityscript.
Also dont forget to drag and drop the go on the public variable.
var myScript:ScriptName = currentObject.GetComponent(ScriptName);
Debug.Log(myScript.description);
I've marked this as correct as though I've NEVER been able to load "Scripts" in JS that way (calling it with : SCRIPTNA$$anonymous$$E). I never made the attempt with C#.
$$anonymous$$y final approach was to do this :
The JS assigned to the prefab with the following data :
var Realname: String;
var description: String;
var price: float;
is called ObjectDB.
Then, to retrieve the data I used:
var propertiesCurrentObject = currentObject.GetComponent(ObjetoDB);
Debug.Log (propertiesCurrentObject.getRealName());
Debug.Log (propertiesCurrentObject.getDescription());
So in a way I was able to make use of both your answers guys, thanks a bunch!
Regards, hope it helps someone else!
Good day.
ScriptName, should name of your script. And that's exactly what you did in your second attempt.
Oh yes, but the problem is on the compiler if I declare punctually as :
var propertiesCurrentObject : ObjectoDB = currentObject.GetComponent(ObjetoDB);
I don't know why, but at least JS doesn't like that call.
Answer by Fearthefrozt · Jul 18, 2014 at 04:15 PM
You would have to access the Realname variable in your other JS script.
Debug.Log(currentObject.Realname);
If this initially gives you an error it's probably because the Realname variable is not accessible from the other script, you could change this by saying:
public var Realname: String;
when you create it, or a better way is to create a getter function in your first script, outside of any other functions that returns Realname:
public function GetRealName(){ return Realname; }
and then call that from the second script where you want to get it:
Debug.Log(currentObject.GetRealName());
The second way is the better way when it comes to security and proper coding techniques.
This may not be the exactly correct code because I usually code in C# but the concept should be correct.
Your answer
Follow this Question
Related Questions
Clone a prefab 1 Answer
Objects flickering between dark and light 7 Answers
Get the name of an instance's prefab at runtime? 0 Answers
Create with code 2.2 pizza not shooting 1 Answer
difference between prefab and gameobject in listening event 1 Answer