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 NeMewSys · Aug 17, 2012 at 03:26 PM · variablevariablessharesharing

Share variables among scripts of the same object

I have a normal game object that represents a lamp, and for it i have a script responsible for turning on/off, and then another script responsible for glowing or unglowing the lamp when it is on or off respectively.

The thing is, inside my turn on/off script I have a status variable (a boolean) that tells its current status (on/off), and I want the other script to have access to it. But how can I do such thing? Scripts can't handle variables of other scripts, even if they're attached to the same object so far i know.

Thanks!

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 NeMewSys · Aug 17, 2012 at 07:52 PM 0
Share

Why can't I up-vote answers? It gives me this stupid message: http://i45.tinypic.com/302uh51.png

avatar image Bunny83 · Aug 17, 2012 at 07:59 PM 0
Share

You can't upvote because you don't have enough karma yet. If everyone can vote up, that would enable spammers to upvote their own spam posts from another account. That's why there's a 15 karma threshold.

Furthermore as long as you have less than 15 karma all your posts will be placed on the moderation queue for verification. This has been implemented to stop the spam on this site.

Next thing is, don't post such annotations as answer. Answers should answer the question. This is a Q&A site, not a forum ;)

I've converted your answer into a comment.

$$anonymous$$ost of these things are explained in the FAQs

avatar image Eric5h5 · Aug 17, 2012 at 11:18 PM 0
Share

> Scripts can't handle variables of other scripts, even if they're attached to the same object so far i know

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

3 Replies

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

Answer by Bunny83 · Aug 17, 2012 at 07:45 PM

Here's a UnityScript example:

 // Toggle script
 // Toggle.js
 
 var state = false;
 
 // [...]

In your other script just do this if you want to read the state variable:

 if (GetComponent(Toggle).state)

If you need to access the variable a lot, or you need other things from that component, save a reference to it. GetComponent have to search for the component on the gameobject eachtime you call it. It's better to setup the references in Start and use the reference directly:

 // another script on the same gameobject
 private var myToggleScript : Toggle;
 
 function Start()
 {
     myToggleScript = GetComponent(Toggle);
 }
 
 function Update()
 {
     if (myToggleScript.state)
     {
         // [...]
     }
 }
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 NeMewSys · Aug 17, 2012 at 10:23 PM 0
Share

Thank you so much! :)

avatar image NeMewSys · Aug 18, 2012 at 11:26 AM 0
Share

By the way, what if I don't know what's the name of the class I'm looking for, what would you put ins$$anonymous$$d of "Toggle" if you didn't know the name of the class?

Because I have a bunch of different scripts (classes) that have that variable, but the obvious way to do it is to make GetComponent() looking for each class to look the one used by the current object.

By other hand, how can I in this conditions in Javascript create inheritance between scripts, for example I have 2 files, "OnOff.js" (1) and "OnOffLamp.js" (2). File (1) defines that variable, and file (2) inherits from file (1). In C# it would be public class OnOffLamp : OnOff { ... } So that when I wanted to get the shared variable I would just need to do GetComponent(OnOff) ins$$anonymous$$d of GetComponent(OnOffLamp) that is too specific.

How can I achieve this? If you say that in background Unity transforms our JS into a class? $$anonymous$$eaning that probably putting something like this in a .js file won't make sense:

class $$anonymous$$yCoolObject extends $$anonymous$$onoBehaviour { var myCoolInt : int; var myCoolFloat : float }

Thanks!

avatar image
2

Answer by ScroodgeM · Aug 17, 2012 at 03:30 PM

//some script

public class Toggle : MonoBehaviour { ... public bool IsEnabled { return ... } }

//other script on same gameobject

public class Glower : MonoBehaviour { ... void GlowEffectApply() { bool lightEnabled = GetComponent<Toggle>().IsEnabled } }

Comment
Add comment · Show 4 · 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 NeMewSys · Aug 17, 2012 at 07:23 PM 0
Share

That's for C#, I'm using JS. If I use a class in a "script.js" attached to an object, where do I instatiate that class then, and where do I put special methods like start() and update()?

Can you do an example like that for JS? And please place start() and update() somewhere so that I can understand where they need to be placed. I know both languages but I'm quite confused about Unity's script attachment logic.

Thanks!

avatar image Bunny83 · Aug 17, 2012 at 07:38 PM 0
Share

You also use classes in UnityScript. You just don't see the class construct because they are automatically generated behind the scenes. Start and Update are class methods

avatar image ScroodgeM · Aug 18, 2012 at 09:21 PM 0
Share

@Ne$$anonymous$$ewSys, if you know both languages, you should know that there's no difference in OOP (like C# is) where do you place methods in code.

if you use js (i don't recommend it if you know C#), the class name is your file name (without '.js')

avatar image NeMewSys · Aug 19, 2012 at 05:00 PM 1
Share

I was confused about the way Unity forces JS plain files to be classes, and how that affected both C# and JS classes. Now in JS I'm declaring the class manually (with public class NameOfTheFile {) so that I can have a better feeling around this and make the code cleaner. I also preffer C# by far but unfortunatly I have to use JS in this project... :(

avatar image
0

Answer by NeMewSys · Aug 18, 2012 at 12:00 PM

Note: I'm posting this as an answer because it is quite an answer to a question here and because I need the Code section or else the linebreaks will get messy.

I've get it, if I define a monobehaviour class inside a .js Unity will not make that conversion. Meaning that in file (1) I can put the OnOff class and then extend it in file (2) and it will behave correctly! So in Unity having this in a "Example.js": ------Code------

 function start() {...}
 function update() {...]


is EXACTLY the same as having this in a "Example.js": ------Code------

 public class Example extends MonoBehaviour {
 function start() {...}
 function update() {...]
 }


And the same goes for C#. This is awesome, now my code will get pretty! :D

Thank you guys!

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 Eric5h5 · Aug 18, 2012 at 12:51 PM 0
Share

You can post formatted code in comments, the same way it's done with answers (4 spaces in front of each line).

avatar image NeMewSys · Aug 18, 2012 at 01:53 PM 0
Share

ah nice! Thanks!

function test() {
return null;
}

Gotta love this community :D

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

11 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

Related Questions

Changing a public varible from another script 3 Answers

Variable Not Changing In Method 1 Answer

Is it possible to save a transform.position in a single variable? 1 Answer

non-static Variable in script on multiple gameObjects reset. Why? 1 Answer

accesing variable from other script 4 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