Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eitanwass · Aug 27, 2016 at 02:23 PM · scripting problembuttontypeinteractionenable and disable script

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mlavik1 · Sep 03, 2016 at 03:30 PM 0
Share

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
 {
      ...
 }
avatar image eitanwass mlavik1 · Sep 03, 2016 at 03:32 PM 0
Share

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?

avatar image mlavik1 eitanwass · Sep 03, 2016 at 07:16 PM 0
Share

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).

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eitanwass · Sep 03, 2016 at 12:52 PM 0
Share

I don't get it... What you wrote is one script?

avatar image Masterio eitanwass · Sep 07, 2016 at 07:09 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i make downloadable content? 1 Answer

[SOLVE] OnMouseEnter not working on UI elements 2 Answers

Several buttons with the same onClick event? 1 Answer

changing script from key based function to GUI button for dice roll game 1 Answer

2 canvases working weird with buttons 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges