LEVEL NOT RESTARTING IN UNITY WHEN I COLLIDE WITH AN OBJECT!!!

Updated on September 15, 2017 in [D] Game Dev. gossip
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on September 14, 2017

Hi, i am a beginner in unity and in C# I have an issue with colliding with objects. Whenever i hit an obstacle/object my game is programmed to restart but it doesn’t when i hit the obstacle the game keeps on going and doesn’t restart but however i also programmed to when it falls out the void it restarts and it does work but colliding with obstacles doesn’t work and there is no coding error that the game can detect so i don’t know whats wrong with my scripts. Can anyone help me please. Here is the coding scripts that are related to the issue (there are 3 of them altogether)

 

1.player movement script:

[ 

 

using UnityEngine;

public class playerprogrammingmovemnt : MonoBehaviour
{

    //This is a reference to the rigidbody component called “rb”
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // We marked this as “fixed”update becuase we are using to mess with physics.
    void FixedUpdate()
    {
        rb.AddForce(00, forwardForce * Time.deltaTime);   //Add a forward force

        if (Input.GetKey(d))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 00, ForceMode.VelocityChange);
        }

        if (Input.GetKey(a))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 00, ForceMode.VelocityChange);
        }

        if (rb.position.y < –1f)
        {
            FindObjectOfType().EndGame();
        }
    }
}
]

[/code]

 

2. Player collision script:

 

[ using UnityEngine;

public class playercollision : MonoBehaviour
{

    public playerprogrammingmovemnt movement;

    void onCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == obstacle)
        {
            movement.enabled = false;
            FindObjectOfType().EndGame();
        }

    }

}
]

[/code]

 

3. Game Manager script:

 

[ using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

    bool gameHasEnded = false;

    public float restartDelay = 1f;

    public void EndGame()

    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log(YOU FAILED);
            Invoke(Restart, restartDelay);
        }
       
    }

   void Restart ()  
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

}
]

[/code]

 

P.S: there is no error’s the game works perfectly. it’s just that whenever i hit an obstacle/object the game is supposed to restart but it doesn’t the only time it restarts is when i fall of the void/out of the world.

  • Liked by
Reply
1 on September 15, 2017

Try to use triggers instead of colliders.

on September 15, 2017

Thanks!!!!

Show more replies
  • Liked by
Reply
Cancel
1 on September 15, 2017

Please don’t use allcaps in the title. It may be that you’re missing a collider or the objects doesn’t collider at all. Also onCollisionEnter isn’t typed correctly. It should be OnCollisionEnter.

on September 15, 2017

Thank you so much it worked!!!!!!!!

Show more replies
  • Liked by
Reply
Cancel