error CS0029: Cannot implicitly convert type `void' to `UnityEngine.GameObject'

I am trying to name a Game object that I spawn into something, but unity gives me this error. here’s my code:

                            Spawn (new Vector3 (0, 1f, 0));
				Spawn (new Vector3 (0, 2f, 0));
				GameObject starA = Spawn (new Vector3 (0, 3f, 0));

can anyone identify the problem?

The error is simply saying that you are assigning a gameObject "starA" to the Spawn function that doesn't return anything (so, void).

1 Answer

1

Spawn is a function that doesn’t return anything, so with this you’re basically telling Unity “GameObject starA is nothing”, which is a problem because you’re also telling it that it’s a GameObject. If you want to create new GameObjects based on a prefab or existing GameObjects try using Instantiate instead.