- Home /
Is there a thing like a type of a script?
Hello human beings!
I am trying to get a script of intractable object e.g. a button or a lever. The thing is that there might be a different script to each of these objects.
So i need to set a type of a script like "InteractableScript" to enable this specific script and not asking an if inside an if....
Is there a way to do that?
Tx. Ethan.
EDIT:
To be more specific and not confuse people:
What i want to do is like so:
this.GetComponent<>(MonoDevelop);
But this will get me all the monodevelop scripts. I want to get all the "Interactable" scripts.
You have made a script (let's say the class name = InteractableObject), and you want to get it using GetComponent()?
If so: this.GetComponent<InteractableObject>();
, will give you the InteractableObject-instance
Your InteractableObject-script could look like this:
public class InteractableObject : $$anonymous$$onoBehaviou
{
...
}
And what to do if i have two or more scripts that are interactable? i don't want to have a loop checking for each of the scripts. Then it leads to my question: can i have a script type to search if there is a script of this type on the object that im clicking?
By "two or more scripts that are interactable", do you mean:
a) You have multiple scripts (classes) inheriting from Interactable ?
b) You have multiple instances of Interactable attached to the same game object?
If a) then you can use GetType() and typeof, as I wrote in my answer below.
If b) then you have to create a field (public string id), and yes.. you have to loop checking for each of the Interactable-components. If you want to do it in one line, you could use Linq: this.GetComponents().Where(t => t.id == "the_id_you_want").FirstOrDefault(); (note that you must add to the beginning of the scripts: using System.Linq).
Answer by dhore · Aug 27, 2016 at 08:23 PM
Sounds like you're talking about Class Inheritance... That's too much to explain here, you'll need to read up... Start off with the MSDN Page here.
Answer by mlavik1 · Aug 29, 2016 at 01:47 PM
I'm not 100% sure I understood your question, but you want to check if the type of an instance equals the type of a class? That's possible!
In c# you have something called Type. This is the specific type of a class.
To get the type of an object, use obj.GetType()
To get the type of a class, use typeof(class)
You may then compare these.
Code Example
I have made a base class called InteractableObject, and two subclasses inheriting from this calss: InteractableWaffle and InteractableChocolate. These are added to a List.
using UnityEngine;
using System.Collections.Generic;
public class InteractableContainer : MonoBehaviour {
public List<InteractableObject> interactableObjects;
void Start()
{
foreach (InteractableObject obj in interactableObjects)
{
Debug.Log(obj.GetType().Name);
if (obj.GetType() == typeof(InteractableWaffle))
Debug.Log("Waffle!");
else if (obj.GetType() == typeof(InteractableChocolate))
Debug.Log("Chocolate!");
}
}
}
Test project can be downloaded here: http://s000.tinyupload.com/index.php?file_id=17246425396535856671 I used Unity 5.3.3
Answer by Masterio · Aug 29, 2016 at 08:25 AM
as dhore said. But I suggest to use interface.
http://www.tutorialspoint.com/csharp/csharp_interfaces.htm
I will give you example (not compiled):
/// IRunable interface, allow to use Run() method in any class what extends it.
public interface IRunable
{
public void Run();
}
/// You can extend any class with IRunable interface (this is example class name).
/// Example: This script can be attached to the doors
public class Doors : MonoBehaviour, IRunable
{
/// You must override the Run() method here
public void Run()
{
Debug.Log("OPENING DOORS");
}
void Update()
{
}
...
}
/// Switch class keeps IRunable implementation as handler.
/// You need to put the RunnableObject in inspector only, or make some game logic here.
/// Example: This script can be attached to the switch object.
public class Switch : MonoBehaviour
{
/// IRunable implementation handler, Unity not support interface property fields in inspector so we will cast it later.
public MonoBehaviour runableObject;
/// Execute this method to run the RunableObject Run() method.
public void Switch()
{
if(runableObject != null && runableObject is IRunable)
((IRunable)runableObject).Run();
}
/// Simple example of switch on trigger enter
void OnTriggerEnter(Collider col)
{
if(col.tag == "Player")
{
Switch();
}
}
}
No, there are 3 scripts. It loks like one :)
First is IRunable interface, it allows you exteds any class with it.
Second is example Doors class, it shows how to use the interface in this class (it can be attached to the any gameObject in scene).
Third is Switch class, it triggers the IRunnable implementation in this case the Doors script (it should be attached to the switch gameObject in scene, dont forget about placing the gameObject with the Doors script to the runableObject variable in inspector).
All that scripts must be in separated files.