I’m hoping you can help me with this problem. I’ve been writing the code for our game for the last 2 months, and in that time, I’ve completely avoided using GameObject.Find, because I’ve never been able to make it work. I have tried every syntax derivation I can think of, and still, it always returns Null.
The funny thing is, we copied a project from my coworker’s computer, and the .Find code that was working there does NOT work on mine, even though the actual objects in the hierarchy, and the code was the same!
Can anyone help shed some light on this? Am I doing something catastrophically wrong?
Here’s the code I’m wrestling with. CoreRoot2 is the name of a game object in the hierarchy. gameLogic is the name of a script attached to that object. I’m using Unity 1.5, but had this same problem on the previous version.
var temp = GameObject.Find("CoreRoot2");
var script = temp.GetComponent(gameLogic) as gameLogic;
All I intend to do is access a public function from the gameLogic script, that’s it. Nothing crazy.
I would start to recommend that you declare your variables.
Automagic does not work with JS on the iphone.
var temp : GameObject = GameObject.Find("CoreRoot2");
var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;
someScript.ThisIsSomeMagicPublicFunction();
gameLogic definitely exists, other scripts are calling static variables from it… Regarding the hierarchy, do I need to call the parent to find the script?
My hierarchy is:
CoreRoot2 (with attached gameLogic script)
– Child 1 (with the current script I’m calling from)
Could it not be working because the script I need is on the parent? I already have CoreRoot2 being called in a variable.
For clarity, this is exactly what I’m working with here:
var temp : GameObject;
temp = GameObject.Find("CoreRoot2");
var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;
if (temp == null) {
print("No gameobject found with that name!");
} else {
print("gameobject: " + temp.name);
if (someScript == null) {
print("No object found with that name!");
} else {
print("someScript name: " + someScript.name);
//someScript.test();
}
}
You need to ask the object that has the component for the component.
if its the parent, don’t use find at all, use transform.parent.gameObject instead.
Also, is the case right?
Sure the class isn’t called GameLogic? (thats normal naming convention, gameLogic would be a variable name)
The script is called gameLogic, is this not a good naming convention for a javascript name?
I’m sorry for my n00bness, but would you please help me with the code for using transform.parent.gameObject? I don’t quite get it. Can I call it as a variable the same way?
Oh man!!! I got it!! Yahooooooo Been working on this for days!
Gentlemen, thanks so much for your help. It’s really cool of you to jump into my problem like this. I hope I can get good enough to pay it forward one of these days.
For anyone else with the same problem, here’s the code that finally worked (thanks to dreamora):
var temp : GameObject;
temp = GameObject.Find("CoreRoot2");
someScript = transform.parent.gameObject.GetComponent(gameLogic) as gameLogic;
someScript.test();