#Making an object visible and invisible using code in Unity

1 messages · Page 1 of 1 (latest)

zenith patio
#

I am new to Unity and not very experienced in it.

eternal temple
#

The reason it doesn't work is because you disable it, you call SetActive(false)

#

Because the game object is disabled, unity messages aren't going to execute, including Update

#

So it never has a chance to reactivate itself

zenith patio
#

how else can I make it invisible otherwise the renderer wants to not work at the time I added it

eternal temple
#

Another object should be in charge, disabling/enabling the object you want to change

zenith patio
#

i have to do it with the code and I don't have enough software knowledge, I've been trying for hours, but there's not much result, all I can do is this, is there not another plugin or something

eternal temple
#

what

winter bay
#

It really isnt that big of a deal dude

#

Just add a serialized reference to the GameObject you want to make visible/invisible and replace your "gameObject.SetActive" with the new reference

#

[]ref

torpid monolithBOT
#

How to get a reference
"How do I access a variable from another script?" is usually one of the first questions we ask when learning Unity. Whether you are C# beginner or a C# professional, understanding the way Unity does things differently can be confusing.

🌳 If the objects are already present in the scene hierarchy...
... then you can use the SerializeField attribute on a field, and assign it by dragging the object which has the script you want to access onto the field slot. This also works if the two scripts are on the same object.

[SerializeField] private SomeScript someScript;

prefab If the objects are instantiated prefabs...
... then you can use a form of injection after you call Instantiate:
SomeScript.cs

public GameManager TheManager { get; set; }

GameManager.cs

var clone = Instantiate(prefab);
clone.GetComponent<SomeScript>().TheManager = this;
winter bay
#

Just read this and you should be good to go