- Home /
 
Component goes away on play?
 I'm trying to attach the game object Player to a script, and it works, but once I enter play mode, it detaches from the script? Is it just some simple thing I'm missing? =/
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.SceneManagement;
public class Portal : MonoBehaviour {
 private bool backToMoon = false;
 int levelToLoad = 1;
 [SerializeField] GameObject Player;
 // Use this for initialization
 void Start () {
     DontDestroyOnLoad(this);
     Player = GetComponent<GameObject>();
     print("Won't be destroyed!");
     if (FindObjectsOfType(GetType()).Length > 1)
     {
         Destroy(gameObject);
     }
 }
 // Update is called once per frame
 void Update () {
 }
 private void LoadScene()
 {
     int doomPortal = Random.Range(0, 100);
     print(doomPortal);
     if (doomPortal == 66)
     {
         EditorSceneManager.LoadScene(2);
         print("Doom World...");
         levelToLoad = 0;
         return;
     }
     if (levelToLoad == 1)
         {
         Player.transform.Translate(0, 0, 0);
         print("Move Character to 0,0,0");
         levelToLoad = 0;
         return;
     }
     if (levelToLoad == 0)
     {
         EditorSceneManager.LoadScene(levelToLoad);
         levelToLoad = 1;
         return;
     }
 }
 /*private void OnCollisionEnter2D(Collision2D collision)
 {
     LoadScene();
     print ("Scene Loaded");
 }
 */
 
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.CompareTag("Player"))
     {
         LoadScene();
         print("Scene Loaded");
     }
 }
 
               }
Answer by UnityCoach · Nov 02, 2018 at 08:26 PM
You're obviously trying to make a Singleton. Although Start() is called on every object in sequence, Destroy doesn't destroy the object immediately, so the following will return true on most objects.
 if (FindObjectsOfType(GetType()).Length > 1)
 
               You rather want to assign a static unique instance member, and self destroy if it's already assigned.
 public static Portal instance;
 void Awake ()
 {
     DontDestroyOnLoad(gameObject);
     if (instance == null)
         instance = this;
     else
         Destroy(gameObject);
 }
 
               Side notes :
GetComponent() makes no sense, use the gameObject property instead
Pass the gameObject to DontDestroyOnLoad instead of the component
Prefer doing this in Awake than Start
Your Player field is a SerializeField, why assigning it upon Start ?
I don't think you need a reference to the Player, as you use a tag to filter the trigger.
I'm still pretty new, so a lot of my code is very sub-par, so thanks for all the tips! The issue was that I had the player in the SerializeField and when I started playmode, it assigned nothing to the field ins$$anonymous$$d, so I just got rid of the line Player = GetComponent();. Thanks for the help! =)
Your answer
 
             Follow this Question
Related Questions
How would I make a ListChangedEvent? 1 Answer
how to change a GameObject from another script 1 Answer
How to always have collision always be on top. 1 Answer
Integer value randomly multiples itself by 3 or 2 for seemingly no reason. 2 Answers
How to move the object to where the object is already pointing to? 1 Answer