- Home /
Finding game object, not working
Trying to use variables of another script by using this: but it is not finding it. Am I supposed to include the .js?
var manager: WeaponManager=GameObject.Find("WeaponManagerScript");
Answer by Julien-Lynge · Jan 08, 2013 at 10:07 PM
Your code is trying to find a gameObject named "WeaponManagerScript". From your question it sounds like you're trying to find a component. so you'll need to use FindObjectOfType instead, and pass in the type (the script name, minus the .js).
http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectOfType.html
First you need a reference to the gameObject containing the script. Once you have that, you can access the component using GetComponent(). Alternatively, you can just look for the component directly (e.g. if there's just one component of that type anywhere in the scene). The latter is what I showed you to do.
Do you already have a reference to the gameObject containing the component you're looking for? If not, you'll need to use Find first to get the game object (pass the name of the game object you're looking for), and then GetComponent.
Alright, now we're getting somewhere :)
First, I'm going to assume that there could be many Weapon$$anonymous$$anagers in the scene. If so, what you'd want to do is the following:
GameObject.Find("Weapon$$anonymous$$anager").GetComponent("Weapon$$anonymous$$anager");
(note that I'm a C# programmer, and this is from memory, so it might not work 100% out of the box)
That finds a game object called Weapon$$anonymous$$anager, and then gets a script on that game object called Weapon$$anonymous$$anager.
If you only have one weapon manager in scene, things get much easier:
FindObjectOfType(Weapon$$anonymous$$anager);
On that one, I'm not 100% sure of the syntax - perhaps somebody with more JS experience can correct it.
GameObject.Find("Weapon$$anonymous$$anager").GetComponent("Weapon$$anonymous$$anager");
this could be problematic and will probably throw a warning. You need to cast or:
GameObject.Find("Weapon$$anonymous$$anager").GetComponent(Weapon$$anonymous$$anager);
@bpears you can read this http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
I would need to see the line and what is around. Also, just try to remove and rewrite the line. It is stupid but for some reasons sometimes the error goes away.
Answer by Great Alexander · Jan 08, 2013 at 10:25 PM
Try to add tag to the objects and use
GameObject.FindObjectWithTag("the object which you want");
Your answer
![](https://koobas.hobune.stream/wayback/20220613090621im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Gameover function calling before game ends help 2 Answers
How to make the camera is not break trough object? 2 Answers
Player instantiates Backwords 0 Answers
A node in a childnode? 1 Answer