A weird story concerning GetComponent() and names of objects.

Hello.
This is the first game I’m working on.
So here I’m going to describe as best as I can something weird that happened.

I want my player’s character to be able to dual wield weapons.
So I created two game objects named “Right Hand Slot” and “Left Hand Slot” each representing a weapon slot for the dual wielding. In each object I added a script “HandSlot” which has the necessary code.
I was trying out ideas for the prototype phase in a scene, and the system worked correctly.

At some point I decided to make a new scene, and so I did.
I added to it all the necessary prefabs, but something was not working correctly. The player couldn’t equip weapons any more. Through debugging I discovered that the lines with this code:

rightHandSlot = GameObject.Find("Right Hand Slot").GetComponent<HandSlot>();

leftHandSlot = GameObject.Find("Left Hand Slot").GetComponent<HandSlot>();

didn’t assign the respective HandSlot components, even though they were finding the game objects that had these components. And that was happening for both scenes.

I tried different things, and eventually I changed the names of the objects that have the HandSlot components from “Right Hand Slot” to “RightHandSlot” and from “Left Hand Slot” to “LeftHandSlot”, and of course I changed the code in the scripts from what it was to:

rightHandSlot = GameObject.Find("RightHandSlot").GetComponent<HandSlot>();

leftHandSlot = GameObject.Find("LeftHandSlot").GetComponent<HandSlot>();

and much to my surprise, that fixed it!

As if for some unknown reason I had to change the name of some random game objects because I created a new scene.

The whole story surprised me so much, I felt I had to share it.

Its pretty bad practise to find objects like this anyway for the exact reasons you posted :slight_smile:

Typically you would want to make rightHandSlot and drag the HandSlot component onto it and make a prefab. That way if you drag that character between scenes its references are all local to itself and will be preserved. Its also much more efficient since GameObject.Find does as you would expect, looks through all the objects in your scene until it finds the one needed, much more efficient if it didn’t need to search at all

1 Like

Never use GameObject.Find. There is always a better way of doing what you’re trying to do.

You’ll also find that Unity likes to add spaces to what it displays in the editor for all sorts of things, where no space exists. For example if you make a script called “MyNewScript.cs” and attach it to a gameobject, the editor will show it as a component named “My New Script”, but if you refer to it with a GetComponent you’ll still need to use GetComponent() without spaces. For this reason I pretty much don’t put spaces in any names anywhere, so I always know what the real name is no matter if Unity pads in spaces or not.

1 Like