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.

Reference gameobject to a prefab script

Discussion in 'Prefabs' started by unity_9362314, Dec 29, 2018.

  1. unity_9362314

    unity_9362314

    Joined:
    Oct 11, 2018
    Posts:
    6
    Hello All,

    I have a portal that is instantiated into a scene after a switch is turned on, and that portal takes the player to the next scene. i also have a scene fader as a gameobject and a prefab but in my portal script it is referenced as public and i have to drag the gameobject to the script.

    my question is, how do i reference the scene fader without having to drag it in the loadnextlevel script on my portal ?

    here are my scripts for both if that can help :

    for the portal (LoadNewLevel) :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class LoadNewLevel : MonoBehaviour
    6. {
    7.  
    8.     public string Level;
    9.     public SceneFader sceneFader;
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.gameObject.tag == "Player")
    13.         {
    14.             Invoke("LoadNextLevel", 1.5f);
    15.         }
    16.  
    17.     }
    18.  
    19.     public void LoadNextLevel()
    20.     {
    21.         sceneFader.FadeTo(Level);
    22.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    23.     }
    24. }
    and for the SceneFader :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SceneFader : MonoBehaviour {
    8.  
    9.     public Image img;
    10.     public AnimationCurve curve;
    11.  
    12.     private void Start()
    13.     {
    14.         StartCoroutine(FadeIn());
    15.     }
    16.  
    17.     public void FadeTo (string scene)
    18.     {
    19.         StartCoroutine(FadeOut(scene));
    20.     }
    21.  
    22.     IEnumerator FadeIn()
    23.     {
    24.         float t = 1f;
    25.         while (t > 0f)
    26.         {
    27.             t -= Time.deltaTime;
    28.             float a = curve.Evaluate(t);
    29.             img.color = new Color (0f, 0f, 0f, a);
    30.             yield return 0;
    31.         }
    32.  
    33.     }
    34.  
    35.     IEnumerator FadeOut(string scene)
    36.     {
    37.         float t = 0f;
    38.         while (t < 1f)
    39.         {
    40.             t += Time.deltaTime;
    41.             float a = curve.Evaluate(t);
    42.             img.color = new Color(0f, 0f, 0f, a);
    43.             yield return 0;
    44.         }
    45.  
    46.         SceneManager.LoadScene(scene);
    47.     }
    48.  
    49. }
    50.