Reputation: 1303
I am working on multiplayer collaboration in unity3d using smartfox server2x . In this there are 2 scripts a)game manager.cs is drag in to game object and b)timer.js script is drag into guitext . How can i add this guitext into game object? When i am trying to do this iam getting an error like
"There is no 'GUIText' attached to the "Game" game object, but a script is trying to access it.
You probably need to add a GUIText to the game object "Game". Or your script needs to check if the component is attached before using it."
This is ma script
#pragma strict
public var timeLeft:float = 10.0f;
public var IsTiming : boolean;
//public bool IsTiming = false;
public function Start () {
//IsTiming = false;
}
function test1(vr1:boolean)
{
IsTiming=true;
Debug.Log("enter33333333333"+IsTiming);
//gamemanager = this.GetComponent("GameManager");
}
function OnGUI()
{
Debug.Log("enter2222222222"+IsTiming);
if(GUI.Button(Rect(0, 70, 150, 25), 'counter'))
{
IsTiming = true;
}
if(IsTiming)
{
Debug.Log("enterzzzzzzzzzz");
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f)
{
guiText.text = "You ran out of time";
guiText.text = "Time is over";
}
else
{
guiText.text = "Time left = " + timeLeft + " seconds";
}
}
}
How can i add this script in to game object "Game".
Upvotes: 1
Views: 6716
Reputation: 6169
The error will be happening because your game object does not have a GUIText component; you need to add one first.
To add a GUIText component, you could write gameObject.AddComponent(GUIText)
within your Start
function.
Or, you could add a GUIText component via the Unity editor menus by going Component > Add..., then type in GUIText.
Also, writing guiText
in your script is shorthand for writing GetComponent(GUIText)
!
Upvotes: 1