- Home /
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?
Is head a static class? With a public member called language?
not =+ you got it the wrong way. Still, what is head and How you get it?
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.
Yeah, unless head is a static class, you don't appear to have a reference to it.
$$anonymous$$gest you watch http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
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.
Thx bro, what should I use with "score" component in game? GetComponent? Could you show me example of 2 scripts with it?
Just click the video link in the comments above. Then scroll down the page. There is a script example there.
yes you can. BUT you should limit it so you don't just do GetComponent in every update when you already got the component.
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.
Your answer
Follow this Question
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