0
\$\begingroup\$

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?
\$\endgroup\$
5
  • \$\begingroup\$ 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 \$\endgroup\$
    – Savlon
    Mar 5, 2015 at 22:36
  • \$\begingroup\$ 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. \$\endgroup\$
    – Zee
    Mar 5, 2015 at 22:42
  • \$\begingroup\$ @Savlon he wants to find a GameObject in the scene... as you said it yourself \$\endgroup\$
    – dimitris93
    Mar 5, 2015 at 22:47
  • \$\begingroup\$ @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. \$\endgroup\$
    – Zee
    Mar 5, 2015 at 22:56
  • 1
    \$\begingroup\$ You didn't mention that in your question. Kindly refrain from being a jerk to people who are trying to help. \$\endgroup\$
    – jhocking
    Mar 5, 2015 at 23:01

3 Answers 3

1
\$\begingroup\$

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>();
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$
        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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .