I am just starting to learn Unity/C#.
One thing I am confused about in the C# scripts is that variables from classes are used, but the classes arn't directly accessed?
For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
void Start(){
Destroy(gameObject);;
}
}
How is gameObject accessed if the class isn't accessed? For example, how is it not:
Destroy(UnityEngine.gameObject) (or something similar)?
I also cannot see the lowercase gameObject on the documentation page.
CodePudding user response:
As various commenters are trying to say; it is part of the MonoBehaviour class, which is accessible for within the Start function, because your Test class inherits everything MonoBehaviour has.
See this documentation: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
Other available properties are:
- runInEditMode
- useGUILayout
- enabled
- isActiveAndEnabled
- gameObject
- tag
- transform
- hideFlags
- name
CodePudding user response:
You are looking at the wrong documentation. The word gameObject here refers to a property of the class Test, that is inherited from MonoBehaviour, which is in turn inherited from Behaviour, which is in turn inherited from Component.
Notice the inheritance clause in the declaration of Test:
public class Test : MonoBehaviour {
^^^^^^^^^^^^^^^
That's what causes Test to gain the gameObject property.
Because gameObject is a member of Test, you do not need to qualify it when you access it. You can just say "gameObject", in the same way that you do not need to prefix the access to foo here:
public class Test : MonoBehaviour { int foo = 10; void Start(){ Debug.Log(foo); // you don't need to write anything before "foo" }
}
