Find is not allowed to be called from a MonoBehaviour constructor

this is my code, I don’t know what to do.

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

public class vehiclList : MonoBehaviour
{
    public static GameObject[]vehicle;
    private int i;

    public GameObject toRotate;
    public float rotateSpeed;

    private void FixedUpdate()
    {
       
        toRotate.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
    }

    public void goGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void goLeft()
    {
        int past = i-1;
        vehicle[i].SetActive(false);
        if(past < 0)
        i=vehicle.Length-1;
        else i--;
        vehicle[i].SetActive(true);
        toRotate = vehicle[i];
    }

    public void goRight()
    {
        int future = i+1;
        vehicle[i].SetActive(false);
        if(future < vehicle.Length)
            i++;
        else i=0;
        vehicle[i].SetActive(true);
        toRotate = vehicle[i];
    }  
}

Are you using new to create one of these objects? You can never do that with MonoBehaviours. You must use .AddComponent() on a valid already-existing GameObject.

The code you posted here and the error are not matching. Are you sure this is the most up-to-date code for this error?

yeah, it’s confusing me to because I’m not even calling Find.

maybe it’s this?

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

public class carSelect : MonoBehaviour
{
    public GameObject[]vehicles;

    void Start()
    {
        int o = vehiclList.i;
        string active = vehiclList.vehicle[o].name;

        foreach(GameObject n in vehicles)
        {
            if(n.name == active)
                n.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

When I use this class I change ‘i’ to public static in ‘vehiclList’.

Is that error the complete error? I mean, copied and pasted?

No I still don’t see it. Try this, when you get the error in the debug console in unity, double tap the line, that should bring up your code editor right at the line where this error happened.
It is possible you’re looking at the wrong file.

Agreed with Fernando on that… I don’t think you’ve isolated what is calling this and causing a problem.

Look for any use of new with a MonoBehaviour (verboten), or look for any use of a static constructor that news something up in the Unity API. That’s not allowed either.

Beyond that, I highly recommend improving your naming convention (vis-a-vis using n for anything other than an indexer), proper capitalization of classes, proper plurality vs singularity, and name stuff fully, not “vehicl” for instance.

I also recommend getting rid of that public static array, unless that’s really what you intend and you understand what it means, rather than just being a lazy way to get at a repository construct.

I can’t double click it because Visual Studio stopped working with Unity for me. The attached screenshot shows the error which I thought pointed to Line 9 in the first class. The error first occurred when I had added two GameObjects at Line 9, each as such: GameObject one = GameObject.Find(“Supra”); I got the error so I deleted those lines, and the error persisted.

Line 9 in both of those is absolutely off-limits for calling GameObject.Find().

ALSO, remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

Fix this ASAP, otherwise you’re working with both hands tied behind your back, typing with your nose. I’m guessing Unity didn’t even recompile your code changes for some reason.

Here’s how:

This may help you with intellisense and possibly other Visual Studio integration problems:

Sometimes the fix is as simple as doing Assets → Open C# Project from Unity. Other times it requires more.

https://discussions.unity.com/t/778503

Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button

Also, this: https://discussions.unity.com/t/805330/7

It’s definitely one of the two classes provided as working on those classes is what brought up the error. Yeah I agree with my variable names they suck. The public static array is an array of the different cars the user can select from. It’s meant to stay the same and be used in multiple classes. Is that not the correct usage? What kinds of problems can be caused by doing it this way?

Alright, VSCode works with Unity now and when I double click on the error it brings me to line 9.

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

public class vehiclList : MonoBehaviour
{
    public static GameObject[]vehicle;
    public static int i;

    public GameObject toRotate;
    public float rotateSpeed;

    private void FixedUpdate()
    {
       
        toRotate.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
    }

    public void goGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void goLeft()
    {
        int past = i-1;
        vehicle[i].SetActive(false);
        if(past < 0)
        i=vehicle.Length-1;
        else i--;
        vehicle[i].SetActive(true);
        toRotate = vehicle[i];
    }

    public void goRight()
    {
        int future = i+1;
        vehicle[i].SetActive(false);
        if(future < vehicle.Length)
            i++;
        else i=0;
        vehicle[i].SetActive(true);
        toRotate = vehicle[i];
    }  
}

What exactly is initializing this? I’m thinking maybe you didn’t used to have this static and now it is, and somehow this has angered the serialization gods?? What happens if you hamburger-reset (upper right corner) this script in your scene? This will dump serialized data for this instance.

Generally statics are to be avoided for this, for a variety of reasons. Make a repository construct, make the entire class static as a singleton, keep all the lifecycle of it consistent.

Well, typically it’s initialized through unity with gameobjects from the same scene. But as of right now the script isn’t being used, I’ve removed it from the only GameObject it was on.

Not if it’s a static!

If you are getting that error still and it’s pointing to a line that should now be fixed. I would say, make sure you have properly saved the script and make sure it’s been recompiled. If you have other compile errors still, Unity might not recompile this script.

Yup, I see that, but fixing it doesn’t seem to help my nonexistent error on Line 9.

It’s being recompiled, because when there are other errors they also show up.

Far fetch but maybe unity isn’t reloading automatically for you. Try pressing Ctrl+R to force reimport changes.