Reputation: 317
I'm trying to send a variable (value?) to function as parameter. The function is in the other script attached on UIRoot (ngui).
Here is my code below.
labelTest script which includes the function:
public class labelTest : MonoBehaviour {
private string val = "hi i'm val";
// Use this for initialization
void Start () {
print ("hello");
}
// Update is called once per frame
void Update () {
print ("update");
//changeLabel ();
//changeLabel (val);
print ("come out changeLabel Func");
}
public void changeLabel () {
print("Im going to change LabelText");
GameObject gol = GameObject.Find ("gol");
UILabel s = gol.GetComponent<UILabel> ();
s.text = "hello";
print (s.text);
}
public void changeLabel (string var) {
print("Im going to change LabelText to var");
GameObject gol = GameObject.Find ("gol");
UILabel s = gol.GetComponent<UILabel> ();
s.text = var;
print (s.text);
}
}
CheckTime script where i have to send a value to function of LabelTest:
using UnityEngine;
using System.Collections;
public class CheckTime : MonoBehaviour {
public delegate void listener( ArrayList touches );
public event listener touchBegin, touchMove, touchEnd;
float touchTime;
void Start () {
print ("checkTime Script Start()");
touchTime = 0;
touchBegin += ( touches ) =>{ Debug.Log( touchTime ); };
touchEnd += ( touches )=>{ Debug.Log( touchTime ); };
touchMove+= ( touches )=>{ Debug.Log( touchTime ); };
}
void Update () {
print ("sssss");
int count = Input.touchCount;
if( count == 0 ) return;
print (count);
bool begin, move, end;
begin = move = end = false;
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
ArrayList result = new ArrayList();
for( int i = 0 ; i<count ; i++ ){
Touch touch = Input.GetTouch(i);
result.Add( touch ); //보낼 인자에 추가
if(touch.phase==TouchPhase.Began&&touchBegin!=null) {begin = true; touchTime += Time.deltaTime; Debug.Log(touchTime);}
else if(touch.phase==TouchPhase.Moved&&touchMove!=null) { move = true; touchTime += Time.deltaTime; Debug.Log(touchTime);}
else if(touch.phase==TouchPhase.Ended&&touchEnd!=null) { end = true; touchTime=0; Debug.Log(touchTime);}
//here!
print ("im in checkTime script");
string s = touchTime.ToString(); //// touchTime -> float to string
go.changeLabel(s);
// NullReferenceException : Object reference not set to an instance of an object CheckTime.Update()
Debug.Log(touchTime);
}
//포인트중 하나라도 상태를 가졌다면..
if( begin ) touchBegin(result);
if( end ) touchEnd(result);
if( move ) touchMove(result);
}
}
What i'm gonna do is that the touchTime (value) placed in CheckTime script is send to changeLabel() function as parameter and showed on Label Text.
However, i've got a NullReferenceException. I checked in script in detailed. How can i fix this? Please let me know what i'm missing now. Thank you.
Upvotes: 0
Views: 97
Reputation: 1792
You can also use singleton approach. If you have only one instance in your project this way can be more useful.
In your MyManager class
public class MyManager : MonoBehaviour
{
public static MyManager instance;
void Awake() {
instance = this;
}
public void MyFunction() {
//funcion code
}
}
Then when you want to call this function from any place in your project simply call
MyManager.instance.MyFunction()
Upvotes: 1
Reputation: 595
Ensure you have a labelTest script attached to the GameObject gol to get your script working, that's the only reason you will be getting a null ref exception just now.
Further from that though, DO NOT keep these two lines in your update function:
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
both GameObject.Find and GetComponent are expensive functions. The best thing for you to do here if you can is have a
public labelTest go;
At the top of your script. Then in the inspector you can assign your label gameObject as this parameter.
If you absolutely have to assign it at runtime then do it in Start instead of Update.
Infact, unless you have further plans for labelTest you can get rid of it all together and use a
public UILabel timeLabel;
at the top of CheckTime and change
string s = touchTime.ToString(); //// touchTime -> float to string
go.changeLabel(s);
to
timeLabel.text = touchTime.ToString();
Upvotes: 2