A Unity3d no le gusta la colisión

using UnityEngine;
using System.Collections;

public class chicken_for : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void FixedUpdate () {

        if (collision.gameObject.Quad == collisionObject){
            Application.LoadLevel("SciFi Level");
        }
    }
}

I am attempting to when A person touches a quad, he goes to this sci-fi fortress. It however says the name 'collision' does not exist in the current context.

preguntado el 28 de mayo de 14 a las 13:05

Of course it doesn't. Where is collision definido? collision is the name of a variable which points to an instance of an object. You will need to have that variable defined somewhere. -

2 Respuestas

You're referencing a variable (collision) that doesn't exist. You've got a variable collisionObject that doesn't exist, too.

collision is typically the name of the argument to the OnCollisionEnter method. Your code should probably be inside this method instead of FixedUpdate. I'm guessing that you've copied code from a tutorial somewhere but put it in the wrong method.

collisionObject on the other hand is harder to guess, but I expect that if your script is intended to be a component on the player object, then collisionObject should be your quad; if the script is on the quad then collisionObject should be the player.

Either way, you need to declare that variable - probably as a public field so that you can populate it from the inspector.

contestado el 28 de mayo de 14 a las 13:05

So your saying... void OnCollisionEnter () { if (collision.gameObject.Quad == collisionObject){ Application.LoadLevel("SciFi Level"); } }? - Gorkan Software

Read the link: the method has a parameter too. void OnCollisionEnter(Collision collision) { ... } - that's one of your two undefined variables. - dan puzey

Add a new function and make sure the player is tagged as player in the inspector. You also need to make sure that there is a collider on the quad that the player touches and a rigidbody component on the player.

using UnityEngine;
using System.Collections;

public class chicken_for : MonoBehaviour {

//This function will handle the collision on your object
void OnCollisionEnter (Collider col){
    if(col.gameobject.tag == "Player"){    //if colliding object if tagged player
        Application.LoadLevel("SciFi Level"); //load the sci-fi level
    }
}
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    }
}

This should get you roughly where you want to be!

Respondido el 15 de diciembre de 15 a las 16:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.