- Home /
Modified Roll a Ball Tutorial,Modified Roll a Ball tutorial question
I did the Roll a Ball Tutorial and was able to get it to work properly, but am working on an assignment for a course based on it, and am not able to get it to count the points properly. I am using a PreFab Teddy Bear (instead of cubes). They are made of several spheres and I attached rigidbodies to them to make them a trigger/ kinetic. When I run the file, it makes different parts of the teddy bear disappear, but the point count isn't consistent. (Sometimes it adds 3 pts, 2 pts, ec.) Is there a way I can combine the Teddy Bear spheres to be 1 object to count as 1 pt, or count each pt when the different spheres collide with the Ball Controller? Below is 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 >= 4)
{
winText.text = "You Win! Yay!";
}
}
}
Answer by Larcondos · Apr 08, 2019 at 04:31 PM
I don't think the error here is with the code itself, but perhaps how you have it set up in your scene. My best guess would be to ensure that only the main object has the script on it, and not each sphere that your bears are made of. That way when you run into the main teddy bear object it should only activate once. Additionally if each of your spheres have a collider/trigger on them, that's probably not necessary. You will only want a collider on your main object (So your teddy bear pickup).
In short, all of your basic spheres should have no scripts attached to them, and no colliders or triggers. Only the main bear object should.