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
0
Question by Griffo · Oct 13, 2013 at 10:34 AM · variabletype

Set variable as type "script"

Hi, I know how to set a variable type as "script" like -

 var buttonsScript : Buttons;

But what I'd like to do is set them in Start() upon the true or false of a boolean.

So what I have in Start() is -

 function Start(){
     buttons = GameObject.Find("GUI Buttons");
 // Is the player playing Multiplayer ?
     if(globalVarsScript.multiplayer){
         buttonsScript = buttons.GetComponent(NetworkButtons);
     }else{
         buttonsScript = buttons.GetComponent(Buttons);
     }

And a part in one of my functions is -

 buttonsScript.touchBegan = false;
 buttonsScript.touchBegan = false;
 buttonsScript.fireBegan = false;

But doing it this way I get the error - Cannot convert 'NetworkButtons' to 'Buttons'. for the line -

 buttonsScript = buttons.GetComponent(NetworkButtons);

Because I thought I could change the variable buttonScript in Start(), is there a way to set the variable buttonScript as type "script" without giving it the script name?

Comment
Add comment
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

1 Reply

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

Answer by gregzo · Oct 13, 2013 at 05:53 PM

You can reference any script by declaring it as a Monobehaviour, but this is not what you need.

Here are 3 options :

1)Have your Buttons calss and NetworkButtons class both extend the same class, let's say BaseButtons for example. This class could be abstract in C#, not sure if you can do that in JS.

2) Have the NetworkButtons extending the Buttons class and overriding methods where appropriate.

3) Have both Buttons and NetworkButtons classes implement an interface, IPressable for example.

It all depends on how different your buttons are. If they are the same buttons, and simply trigger different events, they probably don't even need to be 2 different classes. In that case, simply use SendMessage to link button presses to method calls in other scripts : it's quick and lazy, but perfectly fine for forwarding simple UI events IMO.

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 Jamora · Oct 13, 2013 at 06:03 PM 1
Share

Your #1 works with UnityScript too. you only need to explicitly declare the classes (like in C#) and use the extends keyword.

avatar image gregzo · Oct 13, 2013 at 06:11 PM 0
Share

I haven't touche JS in a while, but seem to remember interfaces work two...

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple objects sharing a boolean variable in a script. 1 Answer

when i access a variable from another script, it doesn't change the variable in that script. what am i doing wrong? 0 Answers

Simple enemy kill count 1 Answer

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer


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