Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Prefab looses all references to objects?

Discussion in 'Scripting' started by foxvalleysoccer, Jun 10, 2016.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    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?

    Code (CSharp):
    1.  
    2. public class GameController : MonoBehaviour
    3. {
    4.     // CONSTANCE
    5.     const string GS_TAG = "GameSpot";
    6.  
    7.     // GENERAL OBJECTS
    8.     public MovePlayer moveplayer;
    9.     public GameObject lightBeam, player, playerSpawnSpot;
    10.     public EnemyManager enemyManager;
    11.     private GameObject enemyCollider;
    12.  
    13.     // GAME SETUP OBJECTS
    14.     static GameController _instance;
    15.     public static GameController Instance
    16.     {
    17.         get { return GameController._instance; }
    18.     }
    19.  
    20.     public GameObject activeStagesPanel, activeEnemiesPanel;
    21.     public GameObject gameBoard, gameInfo, gameOverPanel;
    22.     public GameObject levelText, mobileDPad, questionWindow, playerInfo;
    23.     public GameObject TransitionScreen, TransitionHelpPanel;
    24.     public GameObject spawnLevelBonus, spawnLivesBonus;
    25.  
    26.     public MobileTouchControls touchPad;
    27.  
    28.     // TEXT DISPLAYS AND VARIABLES
    29.     public Button btnRestart;
    30.     public Button btnMainMenu;
    31.     public Text txtTransitionHelp, txtTransitionLevel;
    32.     public Text txtLevel, txtNotify, txtPlayerLives, txtScore;
    33.  
    34.     private int bonusScore = 0, playerScore = 0, pointsPerQuestion = 100, scoreToGiveBonus = 2500;
    35.     public int gameLevel = 1, gameDifficulty, playerLives = 3, SquaresLeft = 28;
    36.  
    37.     // PLAYER MOVEMENT VARIABLES
    38.     public bool canMoveNow, isSettingGame, isBeamActive = false;
    39.     private float levelEndDelay = 5.0f, levelStartDelay = 3.0f;
    40.     private float moveRate = 0.25f, nextMove = 0.0f;
    41.     public GameSpotController activeSpot;
    42.  
    43.     // LIGHTBEAM CALCULATIONS
    44.     static double mean = 1000, rate = 2000, timer = 0, endInterval = 0;
    45.    
    46.     // UI BUTTONS
    47.     public Button padUp, padRight, padDown, padLeft;
    48.     private Vector2 playerMovement = Vector2.zero;
    49.  
    50.     // AUDIO SOUNDS
    51.     private AudioSource source;
    52.     public AudioClip qbertMusic, bassMusic;
    53.     // SOUND FX CLIPS
    54.     public AudioClip alienAbduction, alienBeam, levelComplete;
    55.     // PLAYER SOUND CLIPS
    56.     public AudioClip gameOver, jump, jumpLanding, playerHit;
    57.     // Opponent Pig Sounds FX
    58.     public AudioClip piggyAttack, piggyDeath, piggyDigging, piggyEating, piggyImpact, piggyScream, piggySleeping, piggyTrample;
    59.  
    60.     public readonly Dictionary<Vector3, GameSpotController> _gameSpotsByLocation = new Dictionary<Vector3, GameSpotController>();
    61.     public Material[] gameSpotMaterials;
    62.     public GameObject[] enemyInfoCards, gameSpotInfoCards;
    63. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
    Mycroft and MD_Reptile like this.
  3. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    That helps a bit thanks.