Copy Gameobject variable into another variable

So I am trying to simply copy a variable named tile into another variable named m_tile and it says m_tile is null, when trying to instantiate it, but when I use just the tile variable it works fine. How could I do that?

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

This may also be relevant to your case:

Here is some timing diagram help:

Specifically, explain how you are confident CreateWorld() happens prior to Start(), and then go prove it with Debug.Log()

this didn’t help me at all… I know CreateWorld happens prior Start because the size is set properly and it generates 15x15 tiles. The only error I have is when I set the m_tile to be instantiated instead of tile.

NullRef didn’t change. It’s like War. War Never Changes. Neither does NullRef.

If m_tile is null, you can’t instantiate it. You can’t copy “nothing”. What do you actually want to do?

I want to do what in the picture is, don’t you see it? m_tile = tile, tile is a Gameobject that is not null, but when I use m_tile it is null, why?

A little stupid question, I’m not an expert, but we can write CreateWorld(), without void in front ??

1 Like

Not so much… didn’t even notice that.

OP are you looking for a factory method like this:

Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

https://gist.github.com/kurtdekker/5dbd1d30890c3905adddf4e7ba2b8580

Yeah, they’ve written a private constructor for a monobehaviour, which isn’t correct in the world of Unity.

These values need to be initialised in Awake/Start/OnEnable.