What is the best way for to not display an gameobject into the scene ?
- disable the component Sprite Renderer
- to use renderer.enable = false
- other way ?
Thanks
What is the best way for to not display an gameobject into the scene ?
Thanks
nobody answers me?
It’s up to you which one works best for your game. You can also disable the entire game object with “gameObject.SetActive(false);”.
Those first two are the same thing…
But to do this is not recommended also because GameObject.Find find only active GameObject…
Which works out well enough because using GameObject.Find as your main means of getting references to other objects is also not recommended.
and what you should use instead of GameObject.Find ??
Basic options:
Make a public GameObject reference, drag the target GameObject into it. (Avoids the performance hit, while making the code more versatile) If the referenced object is being instantiated during gameplay, then assign that reference at the time it’s instantiated.
If there’s just one instance of something you need to reference elsewhere, Use the singleton pattern:
public class MyAwesomeSingleton : MonoBehaviour {
public static MyAwesomeSingleton main {
get {
return _main;
}
}
private static MyAwesomeSingleton _main;
void Awake() {
_main = this;
}
}
.....
//anywhere else
MyAwesomeSingleton.main.transform.position = Vector3.zero;
The point is, GameObject.Find is slow (it crawls through EVERY GameObject in the scene, checking the name against the one you supplied), unreliable (if you instantiate the thing you want to find, for example, it now has (Clone) in the name, and your .Find is useless; also, as you’ve noted, it can’t find inactive GameObjects), and less versatile (can’t reuse the same code in other objects). There are times when it’s appropriate to use, but they’re VERY rare.
Ok thank you very much I will remember it for the future works