Unity gameObject setActive hide or enable or disable an object with C# code game dev tips

preview_player
Показать описание
This is a Unity quick tip on how to set a gameObject active . Object.SetActive is an important Unity Method. This is to help anyone from beginners and above .Radiobush has no affiliation to Unity but very much enjoys using the Unity engine.

This tutorial is all about the code lines and how they are used.
Рекомендации по теме
Комментарии
Автор

Great code. Searched very long time solution for toggle button, working in new 2021 Unity! Thanks!

YanusD
Автор

That works, but there a problem, when i clicked first button, the GameObject opens, but when i close it by another button and reopen Gameobject with first button, i didn't work, how can i fix it in my case

marklondon
Автор

ok but when i attach it the the event box in something like a button, the script only gives me the monoscript function. how do I fix this?

tsunderenekokun
Автор

Hello! Mine isn't working. I'm creating a simple textbox that appears when triggered. Here's my error: "member names cannot be the same as their enclosing type".
Here's the Code:

public class TextBox : MonoBehaviour
{
public GameObject TextBox;


void Update()
{
void OnTriggerEnter2D(Collider2D col)
{
if (TextBox.activeSelf != true)
{
TextBox.SetActive(true);
}
else
{
TextBox.SetActive(false);
}
}
}
}

whereswaldo
Автор

Alright, I was hoping this would work for me, but it hasn't.
I've got an array of guns that have children of gun model and muzzle flash.

When I use the below code to activate the inactive muzzleFlash, it doesn't activate.
!= true){

}```

XLineageX