#How to access a GameObject's parent from within a script?

1 messages · Page 1 of 1 (latest)

supple hare
#

I am currently writing in a script that is a component of Beam. Does anyone know how I can access the GameOverUI I tried both creating a normal reference and using GameObject.FindGameObjectWithTag("GameOverUI) but I am still struggling to find a way that works. When I tried to use the FindGameObjectWithTag, Rider showed me this and wouldn't acess it properly.

#

Rider Error VVVV'

frozen token
#

Well it's saying that the GameOverUI tag doesn't exist

#

also the GameOverUI object isn't a parent of Beam

supple hare
frozen token
#

No, the tag must exist

supple hare
#

It does exist

frozen token
#

Are you sure there aren't spaces in the tag name or it's not UL with a lowercase L

#

Rider might also just be disconnected. It's a warning not an error

supple hare
#

Rider usually doesn't have these issues.The autofill typically works just fine.

supple hare
#

Rider still says the same thing. Does C# have anything to access the parent of something?

#

I'm used to Lua where I can do Script.Parent.Parent.Canvas.GameOverUI

#

Idk if Unity has something similar.

frozen token
#

Try if it still works? Just because Rider doesn't find it doesn't mean it's broken

frozen token
#

You can do transform.parent.parent but it's not generally recommended. Optimally the UI manager would be a singleton or you'd assign a direct reference to the UI

supple hare
#

Okay, I'll try that.

granite breach
#

I don't think

supple hare
granite breach
#

What makes you think it's not the problem

#

It definitely doesn't find inactive objects (just checked the docs)

void cosmos
supple hare
stable jacinth
#

You can use gameObject.transform.parent or create singletone and attach your gameobject to public or serialized field and then call your GameOverUI from the singleton.

#

or use search in inactive objects invlude var gameOverUI = Object.FindFirstObjectByType<GameOverUI>(FindObjectsInactive.Include);