1
\$\begingroup\$

I'm trying to make a rolling ball game. But for some reason, I can't seem to get the counter to actually add to the score. Here's the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Movement : MonoBehaviour {
    public float speed;
    public Rigidbody rb;

    private int count;
    public Text countText;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText();
    }

    private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * speed * Time.deltaTime);
    }


    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "item")
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();
        }
    }


    // Update is called once per frame
    void Update () {

    }
}

It's super simple. Like, new new newbie simple. The objects I want to display as collected are tagged "items", they have colliders, and they are kinematic. I've followed the tutorial (this tutorial, specifically: https://www.instructables.com/id/How-to-make-a-simple-game-in-Unity-3D/) to the letter (barring some changes due to Unity updates), but for some reason it's not working.

I'm feeling a lot of anxiety cause I've had panic attacks about failure before and I really don't want to repeat it. Please help!

\$\endgroup\$
1
  • \$\begingroup\$ I notice the same trigger routine that increments the counter also sets the collected item to inactive. Do you observe that the items are deactivating successfully (indicating that the method has run), or are they staying active (indicating the problem isn't in your counter logic, but in the detection of the trigger collision)? And do you have more than one instance of this Movement script in your scene? \$\endgroup\$
    – DMGregory
    Commented Apr 3, 2019 at 2:38

1 Answer 1

0
\$\begingroup\$

I can think of few reasons why this might not work: Are you sure the script is added to the proper object in the game world? Is the collider set properly to "trigger"? Is the collision array set properly? (it's an array where you set which items collide with which. So you can make bullets only react to hitting certain items etc)

And what I think might be the main culprit - didn't you switch around the collider deal? Of course you can do it in many ways, but I think it would be done like this: The sphere has "normal" collider, while the item has a trigger collider. When the sphere enters trigger collider, the item runs the "OnTriggerEnter" method. But it seems like in your script, the sphere has the trigger collider.

Since your sphere most probably only has a "normal" collider, the OnTriggerEnter will never even run!

Another idea: Is the GUIText set up in player settings?

\$\endgroup\$
2
  • \$\begingroup\$ So I just discovered a ridiculous fix to my problem: Turns out the tag wasn't properly written! I had written it to "item" instead of "items"! Still, thank you for answering my question! \$\endgroup\$ Commented Apr 5, 2019 at 18:49
  • \$\begingroup\$ You're welcome, I glad you found it! And it usually is something small, you eventually learn the whole "checklist" and it gets easier eventually ;) Would you minding giving me an upvote and choosing my answer then? \$\endgroup\$
    – lilKriT
    Commented Apr 5, 2019 at 18:52

Not the answer you're looking for? Browse other questions tagged .