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.
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’.
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.
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.
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?
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.
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.