- Home /
script doesnt go off when its called
so im making kinda of an endless runner and i have object that gives "powerups " but when i hit one of the objects it doesnt trigger the effect its needed to do , this is the script of the objects that gives powerups private void OnTriggerEnter(Collider other) {
if (gameObject.layer == 9)
{
gameSession.AddToScore(10f);
Destroy(gameObject);
}
else if (gameObject.layer == 10 )
{
gameSession.getPlayer().GetComponent<Player>().StartSlowingSpeed();
Destroy(gameObject);
}
else if (gameObject.layer == 11)
{
Destroy(gameObject);
gameSession.getPlayer().GetComponent<Player>().StartFloating();
}
}
i have a problem a problem in the ones with layer = 10,11
public void StartSlowingSpeed()
{
Debug.Log("step 2 done ");
StartCoroutine(SlowSpeed());
}
IEnumerator SlowSpeed()
{
addedSpeed -= playerSpeed / 2;
yield return new WaitForSeconds(slowSpeedTime);
addedSpeed = 0;
}
//grape eaten
public void StartFloating()
{
StartCoroutine(FloatUp());
}
IEnumerator FloatUp()
{
jumpMem += 5f;
//gameObject.transform.Translate(Vector3.up * Time.deltaTime * floatHeight);
yield return new WaitForSeconds(jumpHeight);
jumpMem = 0;
yield return new WaitForSeconds(floatTime);
jumpMem -= 5f;
//gameObject.transform.Translate(Vector3.up * Time.deltaTime * -floatHeight);
}
I think you mean to check other.gameObject.layer in your OnTriggerEnter code. What you are doing now is checking if the gameObject that this trigger script is attached to is on layer 9, 10 or 11, you are not checking the “other” collider which I assume are power ups.
I don't think that's the issue since he actually destroys the object this script is attached to. So this is most likely his powerup script. However since he uses different layers it's possible that there may be no collisions between the involved layers. Though it's hard to tell from the information given in the question. We can't do any active debugging. A simple Debug.Log("Powerup pickup. Layer=" + gameObject.layer);
at the beginning of "OnTriggerEnter" should already help to figure out what is happening.
Oops, ya, I failed to notice the gameSession.getPlayer().GetComponent().StartFloating();... But I do see that this line is after you destroy the game object, thus the line will have no effect on the player. You should put it before you destroy the object.