How Can I Get The script On Object via Script Without Using Script Name?

I’m using a list to store object that collided with player. If list isn’t empty an interact button will appear. By clicking the button it should play the script on that object. I’ve set up the script but i don’t know how to get the script from different objects …

→ object are different and have different name so i cant call the script by name

Here my script:

// paste code here

also if (Objects != null) isn’t working - should i use counter or container to check if list isn’t empty?

In Test () method I want to get the script of first object in the list.

So, if i get it right, you want the first objects Component in the list… you can just use

ScriptOfObject script = Objects[0].GetComponent<ScriptOfObject>();

Replace ScriptOfObject with the actual name of the Script.
and if (Objects != null) checks if the list exists (duh, of course it exists), just use

if (Objects.Count != 0)

it checks if it has anything in it

You could make an abstract class called FindableScript.cs

public abstract class FindableScript.cs : MonoBehaviour { /* ... */ }

and then any script that you want to be able to find with your method, you derive from that new abstract class:

public class ScriptA : FindableScript {
  // ...
public class ScriptB : FindableScript {
    // ...

then you will be able to find any derived classes of the abstract one with the normal


and it will return an array of all components of either type ScriptA or ScriptB.