Kaito Kid
Kaito Kid

Reputation: 1073

Unity Figure out the type of an object in scene

I am working with a scene that was created by someone else.

There are some objects in the scene that I need to dynamically reference from a script, but I can't figure out their type.

This might sound dumb, but I can't find any "properties", and the inspector doesn't seem to tell me the type of the selected object, just what components are in it.

So how can I find what Type it is, so that I can use

Component.FindObjectsByType<T>()

in scripts to get it (and a few others that are the same type)?

EDIT: I am using C#, but I am looking for a way, from the scene editor, to find the type of a specific object, so that I can use that type later when I'm writing scripts. For example, some objects are Terrains, Sprites, Cubes, etc.

Upvotes: 1

Views: 1076

Answers (1)

D&#225;vid Florek
D&#225;vid Florek

Reputation: 571

All objects in the scene are of type GameObject. What you are searching for are components. You can learn about components and how they work here in documentation.

If you want to access methods and variables of a component, you can do it multiple ways.

If you want to access a component of a GameObject, you can do it like this:

SpriteRenderer sprite = gameObject.GetComponent<SpriteRenderer>();

If you want to get all components of some type in the scene, you can do it like this:

CharacterController[] controller = FindObjectsOfType<CharacterController>();

Upvotes: 2

Related Questions