# Getcomponent(), GameObject, and Component issues

I have been trying for over 2 hours now to get over these problems :

1. I have 2 GameObjects in my hierarchy, but I do not have them anywhere in any script. I would very much like to add those 2 objects into a script of mine. I used

GameObject thing1 = new GameObject("name of object");

but unfortunately this didn't work, gave me errors about how I can only create objects from main script? I pasted my code into game.cs, and added the game.cs script to the 2 objects I want, but I get the exact same errors.

1. So the reason for this problem is that I need to actually add those 2 objects in my lines of code, so I can use GetComponent(); to be able to modify the enabled status of my script. So the other problem here, is that when I use GetComponent on the 2 objects, I get a nullreference exception. I am very sure that I added the script to both the objects, and I'm pretty sure I didn't mistype them, so can anyone give me a hand please?
• GetComponent returns a component from whatever GameObject your script is attached to... It does not find a GameObject in the scene. I don't really understand your question but maybe you should check out a few basic tutorials for unity Mar 5 '15 at 22:36
• Yes, the syntax is mygameobject.GetComponent<componenttype>(); So I used the GameObject default constructor to try and get the objects I want into my scripts. That didn't work. I tried Find.GameObject("string"), it didn't work either. I tried everything I could have, and then I realised the constructor actually adds the GameObject to my script, the prblem is that GetComponent returns a null when I use GetComponent on the desired Game Object, with the type of the script I used.
– Zee
Mar 5 '15 at 22:42
• @Savlon he wants to find a GameObject in the scene... as you said it yourself Mar 5 '15 at 22:47
• @jhocking I already used that function. I apologize if I express myself in several lines, but I explained my problem as much as possible in my post. Kindly refrain from giving advice that isn't useful to the conversation.
– Zee
Mar 5 '15 at 22:56
• You didn't mention that in your question. Kindly refrain from being a jerk to people who are trying to help. Mar 5 '15 at 23:01

I have 2 GameObjects in my hierarchy, but I do not have them anywhere in any script. I would very much like to add those 2 objects into a script of mine. I used

GameObject thing1 = new GameObject("name of object");


That would create a new GameObject. If you want to get an object by name, you'll need to call

GameObject thing1 = GameObject.Find("name of object");


That will locate the first object by that name. However, if you have multiple objects named that then you're in trouble. It would be better to expose a GameObject field on the component you're writing and link the object in the editor. Just drag the object from the hierarchy into the field on your component in the inspector.

So the reason for this problem is that I need to actually add those 2 objects in my lines of code, so I can use GetComponent(); to be able to modify the enabled status of my script. So the other problem here, is that when I use GetComponent on the 2 objects, I get a nullreference exception. I am very sure that I added the script to both the objects, and I'm pretty sure I didn't mistype them, so can anyone give me a hand please?

If you're trying to get a component named "name of object", then that will return a null because there cannot be a component named such. As above you need to Find the GameObject with that name. Then, to access their components, you'll call

SomeComponent component = thing1.GetComponent<SomeComponent>();


First off, there's this function http://docs.unity3d.com/ScriptReference/GameObject.Find.html

But this may not be the ideal approach in your situation; it's hard to tell. The more typical approach to referencing objects in code is to link them in the Editor. To do that, declare a variable for the object at the top of your code, and then that variable will appear in the Inspector. Now drag your object onto the Inspector.

        Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as Transform;


Believe you want to instantiate a game object. Create a prefab, assign it, and use this command. As far as making a new new object, just make an empty game object, from there you can instantiate all components/renderers/colliders and whatever else