Skip to main content
2 of 2
added code; added 42 characters in body
jhocking
  • 15.8k
  • 2
  • 45
  • 59

Xerosigma's answer points out how you have to address this by using the object reference returned from Instantiate() but he didn't actually explain what you need to do.

The GameObject reference that you get back from the Instantiate() command then has to be passed to object1 (or object2, whichever you are trying to do) in the first script. I'm guessing you typically use those variables by dragging an object in the scene to the slot "object1" in the Inspector; however since the object you want to swap doesn't exist until the game is running, that object can't be referenced by that variable until after the game is running.

That means you can't drag the object in the editor manually; you'll have to set the variable in code. What I'm guessing you want to do is keep a reference to the swap script in the spawn script, and then pass in new objects that way. Something like:

public Swapper myswapper; ... myswapper.object1 = Instantiate(etc.) 
jhocking
  • 15.8k
  • 2
  • 45
  • 59