- Home /
Instantiated Object Not "Colliding" as Expected.
I instantiate five to ten objects from the same prefab. These objects move down a road/trail and eventually pass through a wall (invisible, just there to detect passage). I have a "Counter" that is supposed to increment once each time one of my prefab objects pass through the wall.
The prefab objects have a rigidbody (for physics/movement) and a collider. My invisible wall has a collider.
No matter what I do, I can't get the Counter to add up to more than one (I accidentally got it to add up to two for a short period of time but can't even get it to do that now). I'm trying to figure out what my Inspector settings need to be to get my Counter to increment properly...it's obviously working a teenie bit or it would never get to "1" at all. Hmmmm.
Here's my OnTrigger code:
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag=="Finish")
{
Counter01++;
Debug.Log("Bingo " + Counter01);
}
}
I've split off my "OnTrigger..." portion of the script to its own script and I'm positioning that on different objects to test if that is the problem...I'm putting it on the "finish" cube and I'm putting it on the Instantiated objects, etc. in different combinations.
I'm throwing all my brain cells at this right now (that's probably my main problem right there ;) ) but I'm not having much luck resolving this matter.
I'll keep at it.
I think I'm getting it narrowed down, at least I have it printing "1" over and over (once for each object that passes through). But at one point, it was printing "1" continuously: 999+ times in the console.
$$anonymous$$aybe I haven't had enough coffee yet. Probably something simple that I'm overlooking for some reason.
Seems to be colliding, my Debug.Log will print one time for each instantiated racer...so it's detecting the collision. It's just not incrementing my counter. Hmmmm. I'll post up some code here in a moment. I had to reboot my computer to see if that made any sort of difference. Getting program up now.
Here's my script as last written. I've rewritten it many ways. Not getting anything to work. The following code will print (Debug.Log) "101" one time for each instantiated object that crosses the "finish" line. So it is detecting the objects as they cross, but it is not incrementing my counter. Hmmmm.
public class TriggerScript : $$anonymous$$onoBehaviour
{
private int zCount02=100;
void start()
{
zCount02 = 200;
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag=="Finish")
{
RacerFinished ();
}
}
public void RacerFinished()
{
zCount02++;
Debug.Log ("Done at Last " + zCount02);
}
}
Answer by Topthink · Mar 25, 2018 at 09:28 PM
I figured it out folks. Well, at least I figured a solution that works. I just made it a static variable over on yonder script and then just increment and decrement as necessary from wherever necessary. It will be fine that way because it's only used for a single purpose and will be the same no matter where or when it is used elsewhere.
Thank you for any consideration.
Answer by NoMoneys · Mar 25, 2018 at 06:50 PM
What is "Counter01", a global variable? If it is you might wanna check if you're setting it anywhere else in your code.
Thanks. Good question. It's not a global/static variable.
I've tested it as a public and private variable in different locations in my scripts just in case my problem was with that. Same results. It's picking up the one collision (and in the one case, the two collisions) so I'm pretty sure that is not the problem.
Your answer
Follow this Question
Related Questions
How to make a particle system play on trigger with another box collider 1 Answer
Flying Controls Collision Issues 0 Answers
How to do static collision checks? 1 Answer
Why there is no Collider.IsTouching(...) ? 2 Answers
More realistic physics? 0 Answers