3

Being a student and having finished my school year I studied the development of Unity in C # to learn how to use the engine but also and above all to have fun.

One of the advantages of Unity is that you can pass a game object parameter directly in drag/drop, but for this to be possible it is necessary that the said variable is in public, which goes against what I have learned in class (the attributes must be private as often as possible).

My question was, how to make sure to have the same result with private attributes, i.e., recovered the game object manually without using the drag/drop system?

Thanks in advance and sorry for my English level.

enter image description here

derHugo
  • 83,094
  • 9
  • 75
  • 115
Quentin
  • 486
  • 4
  • 20
  • the reasons to make everything private are much less valid in Unity than in general C#, most of the time you can make pretty much everything public and pay no penalty (other than general mess) – zambari Jun 28 '18 at 07:13

1 Answers1

9

It is not necessary to mark the variable as public in order to make it appear in the Editor. Just just put the SerializeField attribute on top of the private variable and it should show up in the Editor.

[SerializeField]
private GameObject cam1;

You can also do the opposite which is make public variable not show in the Editor. This can be done with the HideInInspector attribute.

[HideInInspector]
public GameObject cam1;

But there is another method in Unity to get a GameObject, by avoiding drag/drop ?

Yes. Many ways.

If the GameObject already exist in the scene then You can with one of the GameObject.FindXXX functions such as GameObject.Find, GameObject.FindGameObjectWithTag

For example,

[HideInInspector]
public GameObject cam1;

// Use this for initialization
void Start()
{
    cam1 = GameObject.Find("Name of GameObject");
}

If the GameObject is just a prefab then put the prefab in a folder named Resources then use Resources.Load to load it. See this for more thorough examples.


Finally if you just want to get the component of that GameObject, just like your cam1 variable which is not a GameObject but a component, First, find the GameObject like I did above then use the GetComponent function to get the component from it.

private Camera cam1;

// Use this for initialization
void Start()
{
    GameObject obj = GameObject.Find("Main Camera");
    cam1 = obj.GetComponent<Camera>();
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Oh thanks for your quick answer. But there is another method in Unity to get a GameObject, by avoiding drag/drop ? I mean by "classic" method like code ? example (totally wrong, just to illustrate): cam1 = *code to access the GameObject* – Quentin Jun 27 '18 at 23:34
  • 1
    Woa thank you so much for your answer that was really helpful :) – Quentin Jun 27 '18 at 23:41
  • 1
    I noticed you may also want to initialize components like the `cam1` variable. See my another edit. You're welcome! – Programmer Jun 27 '18 at 23:46
  • 2
    Brilliant answer! – gotnull Jun 27 '18 at 23:48
  • 1
    Well answered @Programmer. – Milan Egon Votrubec Jun 28 '18 at 00:43
  • You can also use unity's tag feature to find things much more efficiently. Its great to see people taking an interest in proper use of access modifiers. You may want to check out a concept called "dependency injection". – pseudoabdul Jun 28 '18 at 02:12