Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by InevBetrayal · Jan 04, 2014 at 10:17 PM · c#variable

Create a variable of type C# script?

Hi everyone!

In the game i'm making I have interactable objects (let's say for now Doors and Chests).

I have a script that will find the nearest GameObject with the "interact" tag, and if it's within a certain range it will search for the "proximity" script in the object and call a function.

And whenever i press "E" (if in range) it will call the "activate" function on the "proximity" script. This in turn should call the "objectInteract" function on a script i have in the gameObject. This script would change depending on the type of object it was (So Doors have the "door" script added through unity's interface and Chests have the "chest" script added the same way.

My problem is that i can't seem to find a way of having a public variable that will accept any kind of script for me to add through unity's interface.
I know i can have say public ScriptName script, but i wanted a way to have public AnyScript script

Any idea if this is possible?

Comment
Add comment · Show 1
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 frarees · Jan 04, 2014 at 10:35 PM 0
Share

What about $$anonymous$$onoBehaviour? You also have $$anonymous$$onoScript, which references to the actual asset (script text)

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by tanoshimi · Jan 04, 2014 at 10:48 PM

No.

However, the problem you're describing can be solved using the OOP principle of polymorphism.

If you let "door" and "chest" both be derived from a common base class (call it "InteractiveObject", say), and define objectInteract as a virtual method in the base class, that method can be overridden to act in different, specific ways in each derived class.

So have one base class attached to all interactive objects like this:

 public class InteractiveObject
 {
     // Virtual method 
     public virtual void objectInteract()
     {
         // Generic interactive stuff
     }
 }

Then also attach a more specific derived class for each type of object such as:

 public class Door : InteractiveObject
 {
     public override void objectInteract()
     {
         // Specific Interaction code for doors...
 
         // If also want to call generic stuff then add
         base.objectInteract();
     }
 }

or:

 public class Chest : InteractiveObject
 {
     public override void objectInteract()
     {
         // Specific Interaction code for chests...
 
         // If also want to call generic stuff then add
         base.objectInteract();
     }
 }

You can now call the method from the base class script using GetComponent(<InteractiveObject>).objectInteract() on any object and it will actually execute the more specific derived class function depending on whether the object has the Door or Chest script attached.

Comment
Add comment · Show 1 · 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 InevBetrayal · Jan 04, 2014 at 11:49 PM 0
Share

That's exactly what i needed!

I had thought about using a derived class (actually thought about using inheritance) but i wasn't really sure how to implement it, or even if it would work!

Thanks a lot!

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

20 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

Related Questions

Change a Variable with another script not working (C#) 4 Answers

One script, different variable for each object. 1 Answer

Multiple Cars not working 1 Answer

C# Make array based on script variable 1 Answer

How to call Variable across scripts. 2 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