So, I have my walls working, when my Ball collides with them, they turn green, and my score increments… and because I started this one as a breather from the 2D course I challenged myself to code everything ahead, and watch after I had it working, and so far so good…
However, no matter what I try, when I add the additional object, my score stops incrementing…
Example: I have 4 walls, all operating as intended, they get bumped into. turn green, the console lets me know how many times the ball has bumped into something, and the score increments by an additional 100 points…
But, if I add an extra object a with collider and attached the same scripts or even if I just duplicate one of the walls, move it into the center of the area and shrink it down…
As soon as I touch that object, my score increments by 100, the object turns green, and then the score stops incrementing when I touch anything else.
This happens no matter what order I touch the objects, the four outer walls, still increment the score by 100, until i touch he center object. So if i touch two outer walls and then the center object, my score will stop going up at 300, when it should reach 500 (4 walls, and the center object)…
The other objects still turn green, but timesHit counter stops incrementing as if the object was not hit. The objects are all set up the same, as I said it even happens if I just duplicate one of the walls that works. Any ideas on what may be causing this, or tips on how to troubleshoot this?
Without seeing the code it’s hard to guess what’s going on.
As for troubleshooting, what I would do, is try to work out where the problem is, by adding debug statements on each method.
Then I can check if for example a specific method isn’t being called, which narrows the problem down, and I can work on why it’s not being called.
Or the method is being called, in which case I know that something in that method itself is going wrong.
And then work from there, by printing information about the various involved objects, variables, etc into the console.
So for example in this case, absolutely print the timesHit counter, everytime it get’s changed, ideally with a small comment as to where that change is happening.
ie
timesHitCounter++; //just to have an example line of it being changed
Debug.Log($"hit counter value is: {timesHitCounter}, changed in UpdateScore method"); //just as an example for a possible debug statement
Thanks! I am a noob at coding, and have been working through the 2-D course, thought I would see if I could reinforce what I’ve been learning by tackling the 3D course. So every bit of info is helpful. Below are the two relevant scripts, ObjectHit and Scorer. The Scorer Script is attached to my “Player Character,” and the ObjectHit is attached to the 4 walls and the 5th obstacle in the center of the play area. Only the object in the center of the field breaks the score, but its just a duplicate of a prefab, nothing is different.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHit : MonoBehaviour
{
public bool isGreen = false;
public void OnCollisionEnter(Collision other)
{
StartCoroutine(MakeGreen());
}
public IEnumerator MakeGreen()
{ GetComponent<MeshRenderer>().material.color = Color.green;
yield return new WaitForSeconds[1];
isGreen = true;}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scorer : MonoBehaviour
{
[SerializeField] int numHits = 0;
[SerializeField] int score = 0;
ObjectHit objectHit;
private void Update()
{
score = numHits * 100;
}
private void OnCollisionEnter(Collision other)
{
if (!FindObjectOfType<ObjectHit>().isGreen)
{
numHits++;
}
Debug.Log("You've bumped into something, this many times:" + numHits);
}
}
I took a video capture of the behavior in action, if you get a chance, thanks in advance.
https://drive.google.com/file/d/1itZ2Op-oMqpDebsjc7SdumAXHh0SNXIn/view?usp=sharing
Odd behavior indeed.
Your issue is within this block of code:
private void OnCollisionEnter(Collision other)
{
if (!FindObjectOfType<ObjectHit>().isGreen)
{
numHits++;
}
Debug.Log("You've bumped into something, this many times:" + numHits);
}
FindObjectOfType will always return the same object. In this case, your code always return the script of the center object, that’s why it won’t add points after hitting it, it can’t, it’s already green.
You should be using ‘other.transform.GetComponent< ObjectHit >().isGreen’ instead.
Thank you! this fixed it. I believe I understand your explanation. I feel like an old dog, learning new tricks… lol.
Glad it helped. Sorry for the awkward explanation, it’s a little late.
Check the API for a more concise explanation on how FindObjectOfType works, but it can be summarized with this line “Returns the first active loaded object…”, while other.transform.Get… will return the script of the object the ball just collided with.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.