Raycast problem (47603)

Here is my Player Collisions script:

using UnityEngine;
using System.Collections;

public class PlayerCollisions : MonoBehaviour {
    GameObject currentDoor;

    void Update () {
        RaycastHit hit;
        if(Physics.Raycast (transform.position, transform.forward, out hit, 3)) {
            if(hit.collider.gameObject.tag == "playerdoor") {
                currentDoor= hit.collider.gameObject;
                currentDoor.SendMessage ("DoorCheck");
            }
        }
    }

And this Door Manager script:

using UnityEngine;
using System.Collections;

public class DoorManager : MonoBehaviour {
    bool doorIsOpen = false;
    float doorTimer = 0.0f;
    public float doorOpenTime = 3.0f;
    public AudioClip doorOpenSound;
    public AudioClip doorShutSound;

    // Use this for initialization
    void Start () {
        doorTimer = 0.0f;

    }

    // Update is called once per frame
    void Update () {
        if(doorIsOpen){
            doorTimer += Time.deltaTime;
            if(doorTimer > doorOpenTime){
                Door(doorShutSound, false, "doorshut");
                doorTimer = 0.0f; 
            }
        }
    }
    void DoorCheck(){
        if(!doorIsOpen){
            Door(doorOpenSound,true, "dooropen");
        }
    }
    void Door(AudioClip aClip, bool openCheck, string animName){
        audio.PlayOneShot(aClip);
        doorIsOpen = openCheck;
        transform.parent.gameObject.animation.Play(animName);
    }
}

Please format your code by highlighting it and clicking the 101/010 button - I've done it for you this time, but when this is performed after the question is posted symbols are escaped

–

Do you have a collider attached to the door?

–

oh, I just formatted too, sorry. As it's C#, it's all yours =] I would suggest to @sshockrat to place this debug after the if (Physics.RayCast .... to check if there is a collider and if it is tagged correctly (as Mike suggested). Debug.Log("raycast hit tag : " + hit.collider.gameObject.tag);

–

I see you did the decent job of unescaping the symbols though :)

–

Im sorry for the question format. This is my first question in Unity Answers. So i placed debug line and here what the console shows raycast hit tag : playerDoor UnityEngine.Debug:Log(Object) PlayerCollisions:Update() (at Assets/Scripts/PlayerCollisions.cs:21)

–

1 Answer

1

On advising the OP to place Debug.Log("raycast hit tag : " + hit.collider.gameObject.tag); after if (Physics.RayCast …etc… , it is noticeable that the Debug is returning playerDoor , but your tag check is playerdoor : note the capitol D in Door =]

The naming is case-sensitive. Simply change if(hit.collider.gameObject.tag == “playerdoor”) { to

if(hit.collider.gameObject.tag == "playerDoor") {

Its working now. Damn that "case sensitive" THANKS a lot guys. I'm gonna be careful for now on.

–

Now I don't mind, as my goal was to get 2K karma so I could format people's code! But as a guide to completing the question process : when you see an answer you are satisfied with, you can mark the question as 'answered' by clicking on the Tick/Check-mark to the left of the answer (under the thumbs). Then the panel with the answer should have a green background. That's it. It's good to note you also gain karma from accepting an answer. Good luck, have fun, see you on this 'site =]

–