- Home /
Cannot assign GUItext to script?
Hi, I just downloaded Unity and I am currently working through a tutorial on a simple game.
I am trying to assign text(I went Create -> UI -> Text, I hope that was the right one) to this script, but drag and drop as well as typing the name of my text object won't work. I have a feeling that it is because the create menu in hierarchy has UI > Text instead of GUI, since that's the only thing I think may be wrong. My script is the following:
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public float speed; public GUIText countText; private int count;
void Start()
{
count = 0;
setCountText();
}
void setCountText()
{
countText.text = "Count:" + count.ToString ();
}
void Update()
{
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVeritcal = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVeritcal);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count = count + 1;
setCountText();
}
}
}
Answer by Kiwasi · Dec 05, 2014 at 06:03 AM
Not a lot of tutorials have aught up with 4.6 yet. There are some linked onto profile page.
To solve this you need to add
using UnityEngine.UI;
At the top of you script. And change your declaration to
public Text countText;
Everthing else should then work as written.
Your answer
Follow this Question
Related Questions
Turials that work with Unity 3.4 0 Answers
Object doen't move even though i did according lesson 1 Answer
How do I create and animate simple polygons directly from code? 0 Answers
Tutorial button doesn't work 0 Answers
Getting Started Tutorials? 1 Answer