0

Im new to c# and not great at coding in general. Trying to learn. I want this game to detect when my object hits something to see if it hits something that isnt the ground or the finish and then reload the scene if it does. First i tried :

public class wallCollision : MonoBehaviour  
{  

    public Rigidbody rb;
    public Vector3 spawn;

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name != "Ground")
        {
            Scene scene = SceneManager.GetActiveScene(); 
            SceneManager.LoadScene(scene.name);
        }
    }
}

And it worked so then i changed it to this to check for the finish as well.

public class wallCollision : MonoBehaviour
{
    public Rigidbody rb;
    public Vector3 spawn;

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name != "Ground" || "Finish")
        {
            Scene scene = SceneManager.GetActiveScene(); 
            SceneManager.LoadScene(scene.name);
        }
    }
}

It didn't work which is why i need help. I want it to think out like:

If object collides with anything and that isn't ground or finish then.

I know I'm probably doing some simple mistake but i don't know what to search for to find help so if any one can help it would be appreciated. Thanks in advance.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • I think you want something like `col.gameObject.name != "Ground" && col.gameObject.name != "Finish"` – DavidG Dec 24 '18 at 12:56

2 Answers2

0

You have to compare the initial value against both test values:

if (col.gameObject.name != "Ground" && col.gameObject.name != "Finish")
{ }

Another option would be using a collection and Contains, but that is worse from a performance point of view:

if (!(new string[] { "Ground", "Finish" }.Contains(col.gameObject.name)))
{ }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
-1

I need you need something like this instead

col.gameObject.name != "Ground" && col.gameObject.name != "Finish"

Not too sure but is there a code that is .Equals() or something like that

zzdhxu
  • 379
  • 6
  • 22