What is wrong with my code?
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
Please, don't expect us to guess what's wrong.
At least provide an explanation of what doesn't work correctly. For instance, do you get compile-time errors, do you get errors when the code runs, do you get no errors but just not the expected results, or something else entirely?
Why are you asking what's wrong with your code? What is the problem?
Answer by Magius96 · Feb 24, 2016 at 09:39 PM
There are quite a few things that COULD be wrong with your script, but nothing that I can see that is Syntactically wrong with it.
Make sure that you've assigned Text objects to countText and winText in the inspector. Also make sure that you've assigned a speed value in the inspector, I'd advise also assigning a default value in the code to protect you in case you forget to set it in the inspector. Lastly, make sure that you have an object in the scene that has the "Pick Up" tag assigned to it, and that that object has a collision object with it's trigger check box set. If you don't have the trigger check box set then it causes a standard collision instead.
Answer by EliteGamer162 · Feb 29, 2016 at 05:48 PM
well i am a kid and i tried to do as best as i could here is real code im going off of.
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
Are you just asking if you are doing well? If that is the case, them use the forums, Unity Answers is for who needs help, not for feedbacks.