1
votes

I'm taking an online class for game development using unity, the instructor can at sometimes be vague. I am under the impression that using gameobject is the same as using the game objects name(in this case it is MusicPlayer), but when I try and replace MusicPlayer instance with gameobject instance, I get the errorCS0246: The type or namespace name `gameobject' could not be found. Are you missing a using directive or an assembly reference? I am just trying to understand the difference between the two. Thank you in advance.

using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
 void Awake(){
    if (instance != null){
    Destroy(gameObject);
    Debug.Log("Duplicate MusicPlayer Destroyed");
    }

    else{
    instance = this;
    GameObject.DontDestroyOnLoad(gameObject);
    Debug.Log("Original MusicPlayer Created!");
    }
}
}

This is the code that I am getting error with:

using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
public static gameobject instance = null;

void Awake(){
    if (instance != null){
    Destroy(gameObject);
    Debug.Log("Duplicate MusicPlayer Destroyed");
    }

    else{
    instance = this;
    GameObject.DontDestroyOnLoad(gameObject);
    Debug.Log("Original MusicPlayer Created!");
    }
}
}
1
The difference between what? Also add the code you are getting error with.Programmer
The code is there? Am I missing something?Philip Verso
I understand what you meant now, I want to know why I cannot use gameObject and must use the name of the gameObject. (shown in line 4 of both codes)Philip Verso
Well check my answer. It solves your error and shows you why the first code is used instead of the second one.Programmer

1 Answers

3
votes

There is a difference between gameObject and GameObject. Notice the capitalized "G" in the second one.

GameObject is a class. You can create an instance of it like this:

GameObject myobject = new GameObject("PhilipBall");

or you can make it a public variable and assign it from the Editor:

public  GameObject myobject;

gameObject is a variable that is created from GameObject. It is declared like the myobject variable example above.

The reason you can't see it is because it is declared inside a class named Component. Then a class called Behaviour inherits from that Component class. There is another class named MonoBehaviour which inherits from the Behaviour class. Finally, your script which is called MusicPlayer inherits from MonoBehaviour when you did public class MusicPlayer : MonoBehaviour. Because of this you inherit the gameObject variable and can use it.


The gameObject variable type of GameObject class and is used to refer to the GameObject this script is attached to.

I want to know why I cannot use gameObject and must use the name of the gameObject

You can actually do that. Just replace public static gameobject instance = null; with public static GameObject instance = null; then instance = this; with instance = this.gameObject;.

public class MusicPlayer : MonoBehaviour
{
    public static GameObject instance = null;

    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            Debug.Log("Duplicate MusicPlayer Destroyed");
        }

        else
        {
            instance = this.gameObject;
            GameObject.DontDestroyOnLoad(gameObject);
            Debug.Log("Original MusicPlayer Created!");
        }
    }
}

When you use this, you are referring to this script which is MusicPlayer. When you use this.gameObject, you are referring to this GameObject this script is attached to.

Why is public static GameObject instance = null; not used by your instructor?

This is because they want to access the MusicPlayer script variables, functions during run-time.They don't need the GameObject. Now, this can be done with a GameObject but you will have to perform extra steps such as using GetComponent in order to use a variable or call a function in that script.

For example, you have a function named "runFunction" in that script and you want to call it. With the first example, you can just do:

MusicPlayer.instance.runFunction();

With the second example, you have to do:

MusicPlayer.instance.GetComponent<MusicPlayer>().runFunction();

That's the big difference and also note that GetComponent is expensive.