Prefab looses all references to objects?

I think i have two problems.

  1. Once the game is restarted by menu buttons the game adds another instance of my game controller to the Hierarchy.
  2. The new added GameController prefab does not hold all references to my objects there for breaking the game I think…

Why when i drop the GameController into my prefabs does it loose all references?

Does this code below create an new instance of my GameController prefab? How can I stop it from creating a new instance of that GameController?

public class GameController : MonoBehaviour
{
    // CONSTANCE
    const string GS_TAG = "GameSpot";

    // GENERAL OBJECTS
    public MovePlayer moveplayer;
    public GameObject lightBeam, player, playerSpawnSpot;
    public EnemyManager enemyManager;
    private GameObject enemyCollider;

    // GAME SETUP OBJECTS
    static GameController _instance;
    public static GameController Instance
    {
        get { return GameController._instance; }
    }

    public GameObject activeStagesPanel, activeEnemiesPanel;
    public GameObject gameBoard, gameInfo, gameOverPanel;
    public GameObject levelText, mobileDPad, questionWindow, playerInfo;
    public GameObject TransitionScreen, TransitionHelpPanel;
    public GameObject spawnLevelBonus, spawnLivesBonus;

    public MobileTouchControls touchPad;

    // TEXT DISPLAYS AND VARIABLES
    public Button btnRestart;
    public Button btnMainMenu;
    public Text txtTransitionHelp, txtTransitionLevel;
    public Text txtLevel, txtNotify, txtPlayerLives, txtScore;

    private int bonusScore = 0, playerScore = 0, pointsPerQuestion = 100, scoreToGiveBonus = 2500;
    public int gameLevel = 1, gameDifficulty, playerLives = 3, SquaresLeft = 28;

    // PLAYER MOVEMENT VARIABLES
    public bool canMoveNow, isSettingGame, isBeamActive = false;
    private float levelEndDelay = 5.0f, levelStartDelay = 3.0f;
    private float moveRate = 0.25f, nextMove = 0.0f;
    public GameSpotController activeSpot;

    // LIGHTBEAM CALCULATIONS
    static double mean = 1000, rate = 2000, timer = 0, endInterval = 0;
   
    // UI BUTTONS
    public Button padUp, padRight, padDown, padLeft;
    private Vector2 playerMovement = Vector2.zero;

    // AUDIO SOUNDS
    private AudioSource source;
    public AudioClip qbertMusic, bassMusic;
    // SOUND FX CLIPS
    public AudioClip alienAbduction, alienBeam, levelComplete;
    // PLAYER SOUND CLIPS
    public AudioClip gameOver, jump, jumpLanding, playerHit;
    // Opponent Pig Sounds FX
    public AudioClip piggyAttack, piggyDeath, piggyDigging, piggyEating, piggyImpact, piggyScream, piggySleeping, piggyTrample;

    public readonly Dictionary<Vector3, GameSpotController> _gameSpotsByLocation = new Dictionary<Vector3, GameSpotController>();
    public Material[] gameSpotMaterials;
    public GameObject[] enemyInfoCards, gameSpotInfoCards;
}

Prefabs can only retain references to things “within” the prefab (i.e. components on the prefab and/or it’s child gameobjects and their components). When you create a prefab it is no longer “inside” the scene you have active, so the things it had references to elsewhere in the scene simply don’t exist to it anymore.

2 Likes

That helps a bit thanks.