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

Question How does putting return; at line 12 solve my problem of creating an extra gameobject script is attac

Discussion in 'Scripting' started by sahilsharma220202004, Jun 15, 2021.

  1. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Code (CSharp):
    1. public class MainManager : MonoBehaviour
    2. {
    3.     public static MainManager Instance;
    4.  
    5.     public Color TeamColor;
    6.  
    7.     private void Awake()
    8.     {
    9.         if (Instance != null)
    10.         {
    11.             Destroy(gameObject);
    12.             return;
    13.         }
    14.  
    15.         Instance = this;
    16.         DontDestroyOnLoad(gameObject);
    17.  
    18.         LoadColor();
    19.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Looks like you're trying to make a singleton. Be sure you understand how they work in Unity and the lifecycle of things:

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    If you do not apprehend the timing, your code probably won't work reliably.

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Return with a void type basically just stops your code at that return; line. So if your if statement is true, then the part in the if runs, but the rest of Awake isn't going to because it hits the return. Hopefully that answers your question.
     
    Vryken likes this.
  4. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    What is the difference between return null; and return;
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Exactly what it looks like. return null; returns a null value. So if you have a reference type as a return value like this, you might not have an Enemy in range, so you just return null.

    Code (CSharp):
    1.  
    2. public Enemy GetEnemyInRange(float distance)
    3. {
    4.     //Do some code to find if an enemy is in range, if so, return it else return null
    5. }
    6.  
    return; is used as I mentioned, with void to exit a method early with a conditional statement.
     
  6. angel_luis

    angel_luis

    Joined:
    Jun 26, 2016
    Posts:
    17
    Hi, I'm following this Unity tutorial: https://learn.unity.com/tutorial/im...1af7edbc2a0022cdbbb6#60b74182edbc2a54f13d5f2e

    I'm stucked in the part of
    public static MainManager Instance;
    . I know what
    static
    does, but I don't know what
    Instance
    does and after see your post I figure out that we're making a singleton but I don't even know what is a singleton.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Start here:

    https://en.wikipedia.org/wiki/Singleton_pattern

    and go google for some more references.

    Some people hate 'em, some people love 'em, I simply look at them as a tool that is useful for certain things.
     
    angel_luis likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762