How to find out the name of the script on the object?

How to find out the name of the script on the object?,

Using the instruction “FindObjectOfType()”, where T is the type of object we are looking for (in our case it is “SomeScript” type), we can find the reference of the “SomeScript” instance that is assigned to that GameObject, then using the dot operator we can access the GameObject to which that Script is assigned. [Fragomen Connect Client Portal][1] [1]: https://www.fragomenconnect.com/

2 Answers

2

Do you mean programmatically? Because if not you can just check the inspector. But since scripts are components, you could probably do it by getting a list of components attached to the gameObject. This question was previously asked here: Get the scripts name - Questions & Answers - Unity Discussions

Not sure why you need it but:

If you just need the name manually just look in the inspector (as said) otherwise:

An Object could have multiple scripts on it. If you want all of them then call GetComponents with their base type i.e. GetComponents();

Then you can get all their names with something like:


var monoBehaviours = GetComponents<MonoBehaviour>();
var names = new List<string>();
foreach(var monoBehaviour in monoBehaviours) 
{
   names.Add(monoBehaviour.GetType().Name);
}

Something is tingling my spider senses though, it feels like you are trying to get it because you are trying to do something and dont know a better way to do it.