Hi, i am in a bit of a predicament. I was making a script to check if an object is spawned on the “outsideGround” in my game by using an int. I was checking it via an OnTriggerEnter function. for some reason though unity is telling me that the int is both a 1 and a 0 when it is in contact with the “outsideGround” object. here is the simplified version of my code.
using UnityEngine;
using System.Collections;
using UnityStandardAssets._2D;
public class fallingObjectTargetScript : MonoBehaviour {
// Use this for initialization
public int groundCheck = 0; //instantiate the groundCheck variable
void Start ()
{
}
// Update is called once per frame
void Update () {
if (groundCheck ==0)
{
Debug.Log("i am not grounded" +groundCheck); //prints that the object is touching outsideGround and the variables value.
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "outsideGround") //if the object is colliding with an object with the tag outsideGround.
{
groundCheck = 1;
Debug.Log("I am grounded"+ groundCheck); // prints that the object is touching outsideGround and the variables value.
}
}
}
while trying to figure out the issue i also tried changing the int to a bool since i am currently only getting a true or false from this, the issue is still occurring though.
I also put the i am grounded debug.log into the update function at one point to see if it would tell me both there as well and it did…
And please avoid stupid polls. If I could add another poll with the question whether this is a silly poll, the answer would be yes.
Regarding your issue. There can be collisions with more than one object, that means your int changes between 0 and 1. But it will never be both as this is not possible. Another possibility is that you are using the script more than once in your scene and one of the sprites has a collision with that object while the other doesn’t.
using UnityEngine;
using System.Collections;
using UnityStandardAssets._2D;
public class fallingObjectTargetScript : MonoBehaviour {
// Use this for initialization
public int groundCheck = 0; //instantiate the groundCheck variable
void Start ()
{
}
// Update is called once per frame
void Update () {
if (groundCheck ==0)
{
Debug.Log("i am not grounded" +groundCheck); //prints that the object is touching outsideGround and the variables value.
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "outsideGround") //if the object is colliding with an object with the tag outsideGround.
{
groundCheck = 1;
Debug.Log("I am grounded"+ groundCheck); // prints that the object is touching outsideGround and the variables value.
}
}
}
Second, it’s probably not Unity, though I don’t know why that’s happening myself.
Third, note that the inspector refreshes at a lower rate than most things. I think it’s once a second, not sure. Just a note.
Thanks, I fixed the code and removed the poll. As for the collision issue, it shouldnt be changing to 0 though since there is nothing in the code that would switch the variable back to 0. Thanks! the script was attached to another object as well!
Many in this forum are helping and are answering questions. You clearly described your problem, there is no need to add unnecessary noise to your question by adding a poll that doesn’t help solving your issue. Many just have a glance at the questions first and just go ahead if there is noise in the posts.
It is in your own interest not to do that. If you want to discuss, there is a section for that.