Search Unity

Active an Object as a scene is loaded

Discussion in 'Scripting' started by Davide1234, Jun 14, 2018.

  1. Davide1234

    Davide1234

    Joined:
    Jun 30, 2016
    Posts:
    10
    Hi everyone,
    I have created this script. It allows the player to deactivate the StartPanel object by touching it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StaertGame : MonoBehaviour {
    6.  
    7.     public GameObject StartPanel;
    8.     public GameObject VolOn;
    9.     public GameObject VolOff;
    10.     void Awake (){
    11.         Time.timeScale = 0;
    12.         VolOff.SetActive (false);
    13.         VolOn.SetActive (false);
    14.     }
    15.  
    16.     public void startgame () {
    17.         VolOff.SetActive (true);
    18.         VolOn.SetActive (true);
    19.         StartPanel.SetActive (false);
    20.         Time.timeScale = 1;
    21.     }
    22.  
    23.     void Update () {
    24.     }
    25. }
    26.  
    And I have created this script with whom I reload the scene everytime the player dies.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class DestroyerPlayer : MonoBehaviour {
    8.  
    9.     public GameObject score;
    10.     public GameObject highscore;
    11.     public GameObject RespawnPanel;
    12.     public AudioClip mortesound;
    13.     public AudioSource mortesoundSource;
    14.     public GameObject startpanel;
    15.  
    16.     void Start () {
    17.         mortesoundSource.clip = mortesound;
    18.     }
    19.  
    20.  
    21.     void OnTriggerEnter2D (Collider2D Ostacolo) {
    22.         if (Ostacolo.tag == "Ostacolo") {
    23.             mortesoundSource.Play ();
    24.         if (Advertisement.IsReady ("video")) {
    25.                 Advertisement.Show ("video");
    26.         }
    27.         startpanel.SetActive (true);
    28.         score.SetActive (false);
    29.         highscore.SetActive (false);
    30.         Destraa.velocita = -2.5f;
    31.         gameObject.SetActive (false);
    32.         SceneManager.LoadScene ("GiocoScena");
    33.         Score.scoreValue = 0f;
    34.         }
    35.    
    36.     }
    37.     }
    But when I load the scene the StartPanel is deactivated. How can I activate it as the scene reloads?
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Davide1234 likes this.
  3. Davide1234

    Davide1234

    Joined:
    Jun 30, 2016
    Posts:
    10
    I tried your suggestions,but it didn't work. Can you make some examples?
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    something like this should work for you. create a new script called DontDestroyPanel

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class DontDestoryPanel : MonoBehaviour
    5. {
    6.     private static GameObject theDontDestroypanel;
    7.  
    8.     void Awake ()
    9.     {
    10.         DontDestroyOnLoad (this.gameObject);
    11.        
    12.         if (theDontDestroypanel == null)
    13.         {
    14.             theDontDestroypanel = this.gameObject;
    15.         }
    16.         else
    17.         {
    18.             DestroyObject(gameObject.gameObject);
    19.         }
    20.     }
    21.  
    22. }
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using UnityEngine.SceneManagement;
    6. public class DestroyerPlayer : MonoBehaviour
    7. {
    8.     public GameObject score;
    9.     public GameObject highscore;
    10.     public GameObject RespawnPanel;
    11.     public AudioClip mortesound;
    12.     public AudioSource mortesoundSource;
    13.     public GameObject startpanel;
    14.  
    15.     void Awake()
    16.     {
    17.         startpanel = GameObject.Find("nameofthepanel");
    18.     }
    19.    
    20.     void Start ()
    21.     {
    22.         mortesoundSource.clip = mortesound;
    23.     }
    24.  
    25.  
    26.     void OnTriggerEnter2D (Collider2D Ostacolo) {
    27.         if (Ostacolo.tag == "Ostacolo") {
    28.             mortesoundSource.Play ();
    29.         if (Advertisement.IsReady ("video")) {
    30.                 Advertisement.Show ("video");
    31.         }
    32.         startpanel.SetActive (true);
    33.         score.SetActive (false);
    34.         highscore.SetActive (false);
    35.         Destraa.velocita = -2.5f;
    36.         Score.scoreValue = 0f; //i moved this
    37.         SceneManager.LoadScene ("GiocoScena");
    38.         gameObject.SetActive (false); //i moved this,  if this script is off, then the next 2 items would have never been ran
    39.         }
    40.  
    41.     }
    42. }
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class StaertGame : MonoBehaviour
    5. {
    6.     public GameObject StartPanel;
    7.     public GameObject VolOn;
    8.     public GameObject VolOff;
    9.    
    10.     void Awake ()
    11.     {
    12.         Time.timeScale = 0;
    13.         VolOff.SetActive (false);
    14.         VolOn.SetActive (false);
    15.         StartPanel = GameObject.Find("nameofthepanel"); //added this
    16.     }
    17.     public void startgame () {
    18.         VolOff.SetActive (true);
    19.         VolOn.SetActive (true);
    20.         StartPanel.SetActive (false);
    21.         Time.timeScale = 1;
    22.     }
    23.     void Update () {
    24.     }
    25. }