Making object a prefab breaks code.

I’m at my wit’s end. I’m making a racing game. Everything was working fine for a while, but then I made my racer object into a prefab. Suddenly, the player won’t move and other objects can’t see the racer’s script components.

What’s going on?

I’ve attached my game if it helps: Dragon Ball R

I was able to make it playable by adding:


This just parents the camera to the Yajirobe already in scene. I can’t really make out what’s going on in the SpawnRacers script, but it seems you were on the right track. You want a list of racers that will be instantiated on start. And 1 of these racers will be controlled by the player. To do this, you’ll need to do a couple of things: Delete Yajirobe from the scene. Remove the MovementScript from all racer prefabs, so not every single racer is being controlled by the player. Turn off the “PlaceText” object, it’s just going to throw errors for now, the script needs to be updated. But I won’t get into that. The SpawnRacers script needs more work, but to start you off, you can do something like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnRacers : MonoBehaviour
    public GameObject playerRacerPrefab;  // prefab of the racer that will be controlled
    public GameObject[] racerPrefabs;   // other racer prefabs that will be instantiated
	public Transform[] spawners;  // possible spawn locations
    private GameObject playerGameObject;   // reference of the player
    private List<GameObject> racerGameObjects = new List<GameObject>();  // references of the other racers

	// Use this for initialization
	void Start ()
        playerGameObject = Instantiate(playerRacerPrefab); // create player's racer
        playerGameObject.AddComponent<MovementScript>(); // make this racer controlled by player
        playerGameObject.transform.position = spawners[0].position; // position player's racer
        Camera.main.transform.SetParent(playerGameObject.transform); // parent the camera

        // create other races at random spawn locations
        foreach(GameObject racerPrefab in racerPrefabs)
            GameObject tempRacer = Instantiate(racerPrefab);
            tempRacer.transform.position = spawners[Random.Range(1, spawners.Length)].position;

    // Returns the player's racer, can be called from other scripts. ie: FindObjectOfType<SpawnRacers>().GetPlayerRacer();
    public GameObject GetPlayerRacer() 
          return playerGameObject;

Also the AnimationScript is trying to use MovementScript, so this will throw an error on non-player racers. For now, just to get the game to run without errors, you can add this in the update loop, before the script tries to use MovementScript.:

  if (GetComponent<MovementScript>() == null) return;

One more thing, inside of MovementScript, set up some default values, so when SpawnRacer does AddComponent, these values won’t be 0.

public class MovementScript : MonoBehaviour {
	public int speed = 5;
	public int acceleration = 5;
	public int weight = 3;
	public int handling = 4;
	public int boost = 1;
	public int armor = 3;
	public int special = 9;
	public int specialCharge = 10;

Give these changes a shot and see how it works out.