'Find' is not allowed to be called from a MonoBehaviour constructor, call it in awake or start instead (but I did)

337 Views Asked by At

I'm getting the following error:

"UnityException: Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'CombatStateMachine'. See "Script Serialization" page in the Unity Manual for further details. CombatStateMachine..ctor () (at Assets/Scripts/BattleStuff/CombatStateMachine.cs:25)"

The issue is that I AM calling 'find' within the start method of the code:

//using Microsoft.Win32;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CombatStateMachine : MonoBehaviour
{
    public phase currentPhase;
    bool playerCanAct;
    int currentRound;

    public GameObject playerPartyObj;
    public GameObject enemyPartyObj;

    private Queue<string> sentences;

    // Start is called before the first frame update
    void Start()
    {
        currentPhase = phase.start;
        playerCanAct = false;
        currentRound = 0;
        sentences = new Queue<string>();
        playerPartyObj = GameObject.Find("PlayerParty");
        enemyPartyObj = GameObject.Find("EnemyParty");
    StartCoroutine(SetupBattle());

    }



//More code that isn't really relevant AFAICT
}

I've tried commenting out the Find and putting them in the class body, I've tried defining the entire object in the start method (didn't really expect either to work, frankly). But as far as I can tell this thing is giving me an error for not doing something that I've done.

I tried adding [Serializable] to the GameObject declaration (didn't really expect it to work), stated it wasn't valid on this declaration type.

So... what gives?

0

There are 0 best solutions below