- Home /
create GUI.Label and then access to it..
Hi! So need to create GUI.Label and then access to it...
first script :
test.js
Code:
 var style : GUIStyle;
 
 
 public var SCORE = "0";
 
 
 
 function OnGUI () 
 
 
 {
 
 
     style.fontSize=18;
 
 
     style.normal.textColor=Color.red;   
 
 
     GUI.Label (Rect (Screen.width*0.2, Screen.height*0.15, 100, 20), SCORE,style);
 
 
 }
test_access.js
Code:
 function Start()
 
 
 {
 
 
     var LOAD : test;
 
 
     LOAD = GetComponent(test);
 
 
     LOAD.SCORE="1";
 
 
 }
test and test_access - both different object.. error NullReferenceException: Object reference not set to an instance of an object
Answer by T27M · Oct 10, 2012 at 07:34 AM
If you have the scripts on a different object you have to tell it which object to look for.
 var myObject = GameObject.Find("ObjectsNameHere");
 
 LOAD = myObject.GetComponent(test);
 
 
Answer by whydoidoit · Oct 10, 2012 at 07:34 AM
You need something in test_access.js that refers to the component with test.js on it - then call GetComponent on that - or just assign it in the inspector:
test_access.js
   var theOtherObject : test;
 function Start() {
 
      theOtherObject.SCORE = "1";
 
 }
 
Or:
 var otherObject : GameObject;
 
 function Start() {
 
     otherObject.GetComponent(test).SCORE = "1";
 
 }
Please remember naming conventions really help - your scripts (and their inherent classes should start with a capital). Variables should start with a lower case letter and then be Camel case. All upper case implies a constant or preprocessor defined value.
Thanks...
Get something like that to work:
 public var test_obj :GameObject;
 
 function Start()
 {
     test_obj.GetComponent(test).SCORE="1";
 }
So there some question... if i want to shorten line test_obj.GetComponent(test).SCORE
local object SCORE, like
 SCORE=test_obj.GetComponent(test).SCORE;
 SCORE="1";
what object should be SCORE mean
var SCORE : ???????
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                