The door won't be open with the button as it shoulds.

Okay… I managed somehow to open the door model :smile:

Though! I have a problem now… Look at this:


This is the scene. If you need to know anything else, just tell me.

The object I selected, is the button, obviously.

Though, I wanted to let the button open the door, but right now only if I look at the door and push F the door opens, I can only close the door by using the button, but I can’t open it with it.

The Code is the following:

using UnityEngine;
using System.Collections;

public class Doors : MonoBehaviour
{
    public float timeleft = 0;
    public RaycastHit hit;
    public Transform currentdoor;
    public bool open;
    public bool IsOpeningDoor;
    public Transform cam;
    public LayerMask mask;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F) && timeleft <= 0f)
            CheckDoor();

        if (IsOpeningDoor)
            OpenAndCloseDoor();
    }

    public void CheckDoor()
    {
        Debug.Log(Time.frameCount + ": Pressed F");
        if (Physics.Raycast(cam.position, cam.forward, out hit, 5, mask))
        {

           Debug.Log(Time.frameCount + ": Pressed F and closed");

            open = false;
            if (hit.transform.localRotation.eulerAngles.z > 50)
                open = true;

            IsOpeningDoor = true;
           currentdoor = hit.transform.gameObject.GetComponent<Schaltermich>().doorluke.transform;
            Debug.Log(Time.frameCount + ": Hit a " + mask + " (" + currentdoor + ")");
        }
    }

    public void OpenAndCloseDoor()
    {
        timeleft += Time.deltaTime;

        if (open)
            currentdoor.localRotation = Quaternion.Slerp(currentdoor.localRotation, Quaternion.Euler(0, 0, 0), timeleft);
        else
            currentdoor.localRotation = Quaternion.Slerp(currentdoor.localRotation, Quaternion.Euler(0, 0, 90), timeleft);

        if (timeleft > 1)
        {
            timeleft = 0;
            IsOpeningDoor = false;
        }
    }
}

Would be awesome if you could help me.

Best regards
Lance



Well…it tells you the error. Null error on line 36 of Doors.cs.

So, go to that line and see what you are accessing. Assuming the number matches up, your line 36 is

currentdoor = hit.transform.gameObject.GetComponent().doorluke.transform;

So, there are a few things on that line that could be null. I would start with doorluke and then check if the getcomponent is returning null.

Okay… how do I check if it’s returning null?

Component c = GetComponent<Component>();
if(c != null){
   // component was found, safe to use it
}

So you can convert this line:

 hit.transform.gameObject.GetComponent<Schaltermich>().doorluke.transform;

into this:

Shaltermich shaltermich = hit.transform.GetComponent<Shaltermich>();

// does the object have the component, and also check that doorluke is not null (if it can be)
if(shaltermich != null && shaltermich.doorluke != null){
    currentdoor = shaltermich.doorluke.transform
}

And what do I do with this word in line 36?
currentdoor =

Right now I have the following:

                open = true;

            IsOpeningDoor = true;
            currentdoor = hit.transform.gameObject.GetComponent<Schaltermich>().doorluke.transform;

            Schaltermich schaltermich = hit.transform.GetComponent<Schaltermich>();

            // does the object have the component, and also check that doorluke is not null (if it can be)
            if (schaltermich != null && schaltermich.doorluke != null)
            {
                currentdoor = schaltermich.doorluke.transform;
            }

            Debug.Log(Time.frameCount + ": Hit a " + mask + " (" + currentdoor + ")");
        }
    }

He’s showing you how to check if a component is null. Which he then showed you the code that matches it. That was just an example.

1 Like

I need to try again. Sorry, I am still a noob in coding :frowning:

Is there any course I could do?

We all have to start somewhere, however, not much else we can offer. With your current code you need to figure out what is null. That really is the only thing to do unless you want to rewrite your code to something else.