How to get script component of an object with out using its name?

I am having multilple objects having different name and each having single script but different name. I’m having object reference and want to get script attached on it but without using script name. Does anyone know about it?

What you need is to look into interfaces
First you could declare an interface:

public interface IMyInterface
{
    void MethodINeed();
}

then you have all of your other classes, let’s use two of them.

public class ClassA:MonoBehaviour,IMyInterface
{
    public void MethodINeed(){print("This is ClassA object");}
}
public class ClassB:MonoBehaviour,IMyInterface
{
    public void MethodINeed(){print("This is ClassB object");}
}

and now you have your manager somewhere else with the reference to the objects:

public class Manager:MonoBehaviour
{
   public void MethodToFindComponent()
   {
       // Here you have the ref to the object 
       // you don't know if it has ClassA or ClassB
       // Who cares because it has IMyInterface
       IMyInterface script = objReference.GetComponent<IMyInterface>();
       script.MethodINeed();
   }
}

If the object is ClassA it will call the method from ClassA with whatever implementation you made there. If ClassB, then ClassB implementation.

You can also do that with abstract class, the main advantage over interface is that you can drag an abstract class in inspector if it inherits from MonoBehaviour. The downside of it, you can only inherit from one class in C# when you can implements many interfaces.

What do you want to do with those scripts? That influences how we can answer this question. You can get those scripts, but if you want to do anything with them, then you’ll need to do some filtering and casting at some point. This can be done generically, depending on what you’re referencing those scripts for. Remember that Unity’s transforms, mesh filters, colliders, etc, are all scripts too.

You can use .GetComponent < Component > () to get a generic script from there, and cast as you want.

What I’ve put here will search your object for a script (single as you asked) attached to it, and filter out any that you don’t want (transform, etc). It will return the last script attached to it that isn’t filtered out. It would be easier to give you a more specific answer if you outline what you need to use that script reference for.

As others have shown, there are a few ways to do this, but it really depends on what you’re doing it for. This kind of approach is best done with caching in Start (), and can be useful with Reflection when you are doing Editors on variables for mobile testing, and don’t want your designers to have to come to you every time they want to edit another variable on an iOS device.

using UnityEngine;
using System.Collections;

public class ScriptGetter : MonoBehaviour {
	
	public	GameObject []	objects;    //  Objects you want to get scripts from


	public void Start () {
		scripts	= new Component [ objects.Length ];

        //  Find the Scripts in the GameObjects
		for ( int i = 0 ; i < scripts.Length ; i++ ) {
			Component [] scriptsOnObject	= objects [ i ].GetComponents < Component > ();

            //  Search each script on the component, and filter out default Unity ones
			for ( int j = 0 ; j < scriptsOnObject.Length ; j++ ) {
				if ( !UnwantedTypes ( scriptsOnObject [ j ] ) ) {
					scripts [ i ] = scriptsOnObject [ j ];
				}
			}
		}

        //  Display our scripts from our objects
		for ( int i = 0 ; i < scripts.Length ; i++ ) {
			Debug.Log ( scripts [ i ] );
		}
	}


	private	Component []	scripts;  //  Our cached Scripts will live here

	private System.Type [] unwantedTypes = { typeof ( Transform ) , typeof ( Collider ) , 
									 		 typeof ( MeshRenderer ) , typeof ( MeshFilter ) };
	
    //  Only allow types we want through
	private bool UnwantedTypes ( Component _component ) {
		for ( int i = 0 ; i < unwantedTypes.Length ; i++ ) {
			if ( _component.GetType ().Equals ( unwantedTypes [ i ] ) ) {
			    return true;
			}
		}
		return false;
	}

}