wrong return
Hi guys....I have a problem with with a return in my code... this is my code....everything is okay but CoinMove method doesn't return 1 and return 0.... my code is in the pic
public class Cube : MonoBehaviour { [SerializeField] float xVelocitySpeed = 5f; [SerializeField] float yVelocitySpeed = 0f; Rigidbody2D cubeRigidbody;
[SerializeField] GameObject cubeStarterPrefab;
[SerializeField] float cubeDelayToMove;
private bool ImIn = false;
public int CoinsPlusOne = 0;
// Start is called before the first frame update
void Start()
{
cubeRigidbody = GetComponent<Rigidbody2D>();
CoinMove();
}
// Update is called once per frame
void Update()
{
Invoke("MoveCubes", cubeDelayToMove);
//if (Input.GetButtonDown("Fire1"))
//{
// analyzer();
//}
analyzer();
}
public void MoveCubes()
{
cubeRigidbody.velocity = new Vector2(xVelocitySpeed, yVelocitySpeed);
}
void OnTriggerEnter2D(Collider2D wall)
{
if(wall.name == "in")
{
ImIn = true;
}
else if(wall.name == "out")
{
ImIn = false;
}
else
{
DestroyObject(gameObject);
}
}
public void analyzer()
{
if(ImIn == true && gameObject.tag == "WhiteCubes")
{
CoinsPlusOne += 1;
ImIn = false;
}
else if(ImIn == true && gameObject.tag != "WhiteCubes")
{
return;
}
else
{
return;
}
}
public int CoinMove()
{
return CoinsPlusOne;
}
}
add a debug.log() in coinmove and in the analyzer and check whats getting called before
Answer by ChrisD0 · Jun 25, 2019 at 10:22 AM
It is the if statement in the analyzer that increments CoinsPlusOne. But look at the condition; and gameObject.tag == "WhiteCubes". When you use gameObject in code, it references the object that the script is attached to. Are you trying to use it to check the tag of the object that was collided with?
Follow this Question
Related Questions
How do I reference an incremented int with one button, that was incremented from another button? 1 Answer
Player controls and UI Controls don't work together in the same scene 0 Answers
Getting crash on specific devices? 0 Answers
MLAGENTS: Is it possible to omit a failed agent's data from learning model? 0 Answers
On collision with enemy slow player down while inside the collision box, else normal speed. 0 Answers