Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Find is not allowed to be called from a MonoBehaviour constructor

Discussion in 'Scripting' started by MosephHD, Jun 24, 2020.

  1. MosephHD

    MosephHD

    Joined:
    Jan 12, 2020
    Posts:
    5
    I'm trying to create a final build of one of my first games and it works perfectly in editor mode, however I can't create the final build because I have a few of these errors.

    Here is an example of my code, I would appreciate someone who can explain why this happens and help correct it.

    Code (CSharp):
    1. public class EndTextTrigger : MonoBehaviour
    2. {
    3.     public Text EndText;
    4.     public float time;
    5.     public bool beenInRadius = false;
    6.     public GameObject Harness = GameObject.Find("Harness");
    7.  
    8.     void Start()
    9.     {
    10.         EndText.gameObject.SetActive(false);
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.  
    16.     }
    17.  
    18.     void OnTriggerEnter(Collider player)
    19.     {
    20.         beenInRadius = true;
    21.  
    22.         if(Harness.layer == 12 && beenInRadius == true)
    23.         {
    24.             EndText.gameObject.SetActive(true);
    25.         }
    26.  
    27.     }
    28.  
    29.     void OnTriggerExit(Collider player)
    30.     {
    31.         EndText.gameObject.SetActive(false);
    32.     }
    33. }
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    that looks to me not right:

    Code (CSharp):
    1. public GameObject Harness = GameObject.Find("Harness");
    you need to do it in the "start" or "awake" function instead of in the declaration
     
    MosephHD likes this.
  3. MosephHD

    MosephHD

    Joined:
    Jan 12, 2020
    Posts:
    5
    Thanks for the help,

    If I declare it in Awake() and Start(), it throws out another error "The name 'Harness' doesn't not exist in the current context"
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    You don't declare it in Awake or Start.

    You declare it as a class level field/variable still as you already have.

    You assign it in Awake or Start.

    Code (csharp):
    1.  
    2. public class EndTextTrigger : MonoBehaviour
    3. {
    4.     public Text EndText;
    5.     public float time;
    6.     public bool beenInRadius = false;
    7.     public GameObject Harness;
    8.  
    9.     void Start()
    10.     {
    11.         Harness = GameObject.Find("Harness");
    12.         EndText.gameObject.SetActive(false);
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.  
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider player)
    21.     {
    22.         beenInRadius = true;
    23.  
    24.         if(Harness.layer == 12 && beenInRadius == true)
    25.         {
    26.             EndText.gameObject.SetActive(true);
    27.         }
    28.  
    29.     }
    30.  
    31.     void OnTriggerExit(Collider player)
    32.     {
    33.         EndText.gameObject.SetActive(false);
    34.     }
    35. }
    36.  
     
    MosephHD likes this.
  5. MosephHD

    MosephHD

    Joined:
    Jan 12, 2020
    Posts:
    5
    Thank you, you're a legend.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Also remove the empty Update methods. They are actually making your game slower and consuming more energy than needed.