GameObject.Find not working

I have no idea why GameObject.Find isnt working. I’ve made sure every name is correct both in the inspector and in the classes that are populating the list. I then commented that out and tried to directly type in the name of the game object it is searching for as seen below and it still did not work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleManager : MonoBehaviour
{
    public enum PerformAction
    {
        WAIT,
        TAKEACTION,
        PERFORMACTION

    }
    public PerformAction battleStates;
    public List<HandleTurn> PerformList = new List<HandleTurn>();
    public List<GameObject> HeroesInBattle = new List<GameObject>();
    public List<GameObject> EnemiesInBattle = new List<GameObject>();
    public GameObject performer;
    // Start is called before the first frame update
    void Start()
    {
        battleStates = PerformAction.WAIT;
        EnemiesInBattle.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
        HeroesInBattle.AddRange(GameObject.FindGameObjectsWithTag("Hero"));
    }

    // Update is called once per frame
    void Update()
    {
        switch(battleStates)
        {
            case (PerformAction.WAIT):
                if (PerformList.Count > 0)
                {
                    battleStates = PerformAction.TAKEACTION;
                }
                break;
            case (PerformAction.PERFORMACTION):
                break;
            case (PerformAction.TAKEACTION):
                //performer = GameObject.Find(PerformList[0].Attacker);
                //testing GameObject.Find
                performer = GameObject.Find("Enemy 2");
                Debug.Log(PerformList[0].Attacker);
                if(PerformList[0].Type == "Enemy")
                {
                   
                    //EnemyStateMachine ESM = performer.GetComponent<EnemyStateMachine>();
                    //ESM.HeroToAttack = PerformList[0].AttackersTarget;
                   // ESM.currentState = EnemyStateMachine.TurnState.ACTION;
                }
                if (PerformList[0].Type == "Hero")
                {
                   
                }
                break;

        }
    }

    public void CollectActions(HandleTurn input)
    {
        PerformList.Add(input);
    }
}

Can you share what your scene hierarchy looks like when this code is running?

Wild thought, but maybe at Start() there are no game objects with those tags? Maybe try changing “void Start()” to “void Awake()” and then be sure that the BattleManager script runs after you have the enemies spawned in.

They’re definitely tagged correctly I checked.

EDIT: The parts of the script that use the tags are functioning normally. It’s this part that uses a string for the name that isn’t. For some reason I can debug.log out the string, but passing any kind of string or even hard coding the correct one in as seen in my code is not working with GameObject.Find

6216617--683492--Unity.png

Ok so… what part of this isn’t working exactly? Are you getting some kind of error at some point?

GameObject.Find should be putting “Enemy 2” into the underlined space. I commented the code out causing the Null Exception Error so I could debug if it was even passing a string or the if it was passing the incorrect one. It was correct, so I thought it might be some kind of weird unity error, so I typed in the string that should be passed in as seen above. It still did not find the game object “Enemy 2”

Performer should be receiving that game object where the red underline is

6216632--683495--unity 2.png

Not completely sure why Find isn’t working here…

However, I’m noticing that your Perform List actually already has a reference directly to the GameObject in it. Is there any reason you’re using GameObject.Find on the name instead of just directly using the GameObject reference that’s already in your list?

In your code it would probably look like this (I can’t see the full name of the field so I’m guessing here that it’s “AttackersGameObject”):

performer = PerformList[0].AttackersGameObject;

This would be a big performance boost over GameObject.Find too.

Im gonna try doing it this way and hopefully it works just as well. I was mostly just wanting to use the names because I thought it would be easier to keep track off.

I’m still super confused as to why .Find is failing. I’ve never seen it behave this way before and would like to find out why.

Save the result of the Find in a variable and print out its name. That should settle once and for all whether it’s actually working or not.

One possibility is maybe there’s some kind of invisible character in the name of the object or something? Or maybe some other script is setting the reference back to null??

But yeah… based on the information you’ve given, I’m kinda stumped! It should totally work. It’s definitely not the code I would write (Find is quite slow, especially to use it EVERY FRAME like that), but it should work!

Well really when its done it should only run once at the beginning of each enemy turn. But I’ll play around with that and get back to you.

I think it was an invisible character, though I’m not sure how it got there. I think I retyped the names once or twice.

1 Like