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 Weither · Sep 28, 2014 at 04:53 PM · functionmonobehaviour

communicating between scripts doesnt work

 using UnityEngine;
 using System.Collections;
 
 public class englishorpolish : MonoBehaviour
 {
     void OnGUI ()
     {
         if (GUI.Button (new Rect ((Screen.width+50)/2, (Screen.height)/2,100,50), "Polski", GUI.skin.GetStyle ("button"))) 
         {
             head.language = 1;
             Application.LoadLevel("0. mainmenu");
         }
         if (GUI.Button (new Rect ((Screen.width-200)/2,(Screen.height)/2,100,50), "English", GUI.skin.GetStyle ("button"))) 
         {
             head.language =+ 1;
             Application.LoadLevel("0. mainmenu");
         }
     }
 
 }

nothing happens in head's script(it doesnt add anything). What I did wrong?

Comment
Add comment · Show 8
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 tanoshimi · Sep 28, 2014 at 06:01 PM 0
Share

Is head a static class? With a public member called language?

avatar image fafase · Sep 28, 2014 at 06:10 PM 0
Share
  • not =+ you got it the wrong way. Still, what is head and How you get it?

avatar image Kiwasi · Sep 28, 2014 at 06:28 PM 0
Share

Why +=? Explicitly stating 2 would be better.

If this compiles then that's your problem. However without the head script it's impossible to tell.

avatar image theredace · Sep 28, 2014 at 08:00 PM 0
Share

Yeah, unless head is a static class, you don't appear to have a reference to it.

avatar image tanoshimi · Sep 28, 2014 at 09:16 PM 1
Share

$$anonymous$$gest you watch http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Sep 28, 2014 at 11:13 PM

Language settings are one of the few places static actually makes sense. Assuming your game is to be played in a single language once the setting is chosen. If you have multiple different characters speaking different languages you need to use GetComponent.

Here is working code using static

 using UnityEngine;
 using System.Collections;
  
 public class englishorpolish : MonoBehaviour {
     void OnGUI () {
         if (GUI.Button (new Rect ((Screen.width+50)/2, (Screen.height)/2,100,50), "Polski", GUI.skin.GetStyle ("button"))) {
             Head.language = 1;
         }
         if (GUI.Button (new Rect ((Screen.width-200)/2,(Screen.height)/2,100,50), "English", GUI.skin.GetStyle ("button"))) {
             Head.language = 2;
         }
     }
 }
 
 public class Head {
     public static int language;
 }

Note: I strongly suspect your problem had nothing to do with accessing the variable, and everything to do with assigning it via +=. Your original code would increase the language by one. As ints default to zero I suspect both buttons were working but doing the dame thing.

Note: By convention class, struct and method names should start with a capital letter. Field, variable and property names start with a small letter. This will make it easier for others to read and assist.

Comment
Add comment · Show 7 · 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 Weither · Sep 29, 2014 at 04:44 AM 0
Share

Thx bro, what should I use with "score" component in game? GetComponent? Could you show me example of 2 scripts with it?

avatar image Kiwasi · Sep 29, 2014 at 06:33 AM 0
Share

Just click the video link in the comments above. Then scroll down the page. There is a script example there.

avatar image Weither · Sep 29, 2014 at 12:55 PM 0
Share

Can I use GetComponent in Update() ?

avatar image scnoobi · Sep 29, 2014 at 12:58 PM 0
Share

yes you can. BUT you should limit it so you don't just do GetComponent in every update when you already got the component.

avatar image theredace · Sep 29, 2014 at 01:11 PM 0
Share

Yeah, GetComponent is performance intensive. Once or twice here or there is fine, but definitely don't do it every frame. Do it when you need it, store the reference in a variable, and then just use the variable after that.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Possible to play single clips using Mecanim? 0 Answers

using non static variable in static function 1 Answer

How to call functions from update function 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