"The variable (variable name) of (script) has not been assigned." when I use a GameObject variable in a different method

So at the start of my code I assigned a GameObject to a prefab but since I can’t attach objects to prefabs, I set it in the code

player = GameObject.FindWithTag("Player");
spawner = GameObject.FindWithTag("Spawner");

Now the player one works perfectly, and so does the Spawner one until I tried using that GameObject in a different method, it says it hasn’t been assigned (the code I put above is in my Start method). How can I access the GameObject in different methods?

Sounds like it isn’t finding the spawner. Either it isn’t tagged or it isn’t in the scene when this object is initialising.

Try not to use tags or GameObject.FindWithTag. In general you should avoid all .Find and similar methods.

But if you must use one, it should generally always be Object.FindObjectOfType<T>, or the newer Object.FindFirstObjectByType<T>: Unity - Scripting API: Object.FindFirstObjectByType

You always care about the components, and the game object is just a container for components. Go straight for the components and skip the game objects.

Nonetheless this is just general advice. Without seeing your code or knowing more about the set-up I can’t be more specific. You at least already know what is null, you just need to determine why its null.

Ok that makes sense but I said it works perfectly everywhere EXCEPT that one method, so I’m trying to figure out why it’s not working in that specific method and how to fix that, so it’s not an issue with it not being on the object, if that helps.

Again, drag the stuff in if you can, it works every time.

If these are singletons or service-y things, see bottom of this post.

Here’s a handy checklist for you if you insist on crazy ninja code:

Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

Here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:

  • what you’re looking for:
    → one particular thing?
    → many things?
  • where it might be located (what GameObject?)
  • where the Get/Find command will look:
    → on one GameObject? Which one? Do you have a reference to it?
    → on every GameObject?
    → on a subset of GameObjects?
  • what criteria must be met for something to be found (enabled, named, etc.)
  • if your code expects one instance and later you have many (intentional or accidental), does it handle it?

If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.

This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.

If you run into an issue with any of these calls, start with the documentation to understand why.

There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.


Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance

Alternately you could start one up with a [RuntimeInitializeOnLoad] attribute.

There is never a reason to drag a GameObject into a scene if it will be DontDestroyOnLoad.

If you do:

  • you may drag it into something else that isn’t DDOL. FAIL
  • you may drag something else into it that isn’t DDOL. FAIL

Just DO NOT drag anything into a scene that will be marked DontDestroyOnLoad. Just Don’t Do It!

I would absolutely LOVE to do that but with prefabs I can’t just drag hierarchy objects in, I have to use the code. I want to drag my spawner onto the prefab but it just won’t let me. I’m super new to Unity so I barely know anything honestly. Does the spawn manager being an empty object have to do with anything?

Have you confirmed what is actually unassigned? Is there another reference type in the line where the error is thrown that could be missing a reference? Have you tried connecting the debugger to step through the logic?

An unassigned error means you have a serialised Unity object field that doesn’t have an object assigned. It’s one of Unity special fake-null objects that give us more informative errors. It could be another game object with the error, it could be an errant extra instance of the component that is missing an assigned field.

Again without seeing the code we can only guess.

1 Like

I know what’s unassigned, and the only problem the code is having is that I’m referencing that object. I’ll mess around with it more tomorrow but I think that’s the only line referencing that object, so it has to be a problem with how I set up the variables at the beginning.

Ensure that your spawner variable is declared at the class level (outside any method) and not as a local variable within the ‘Start’ method. This way, it retains its reference and can be accessed by other methods in your script.