Accessing component on another Object.

Hello to everyone I made two scripts one should hit collider in one side of the room, what will activate animation on the object it another side of the room. Raycast works good but when hit Input button it give a Nullreference tried to google but it didn’t helped.

Can someone help please.

Here it is RayCast Script

    private float CeilingDistance = 5f;
    public phoneAnimationScript myphoneAnimationScript;
    public phoneAnimationScript myCharacterAnimationScript;
    public CeilingDoorAnimCtrl CeilingDoorsCtrl;
   
    Camera CameraMain;
   
   public void Start()
    {
        CameraMain = GetComponent<Camera>();
    }
    public void Update()
    { 
RaycastHit CeilingButton;
        Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
            if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
            {
                if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
                {
                   
                    if (Input.GetButtonDown("actButton"))
                    {
                        CeilingDoorAnimCtrl cas = GetComponent<CeilingDoorAnimCtrl>();
                        CeilingButton.collider.gameObject.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
                    }
                    Debug.Log("hitCollider");
                }
            }
        }
    }

And the Animation Code

public class CeilingDoorAnimCtrl : MonoBehaviour {
    private Animator CeilingAnimator;
    static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
    static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
    // Use this for initialization
    void Start () {
        CeilingAnimator = GetComponent<Animator>();
    }
   
    public void OpenDoors()
    {
        CeilingAnimator.SetBool("Open", true);
    }
    public void ClosedDoors()
    {
        CeilingAnimator.SetBool("Close", true);
    }
}

Please include the exact error with the line it’s occurring on.

I’d also say try to stick you a consistent naming scheme. Some of you’re classes/types are capitalised, some aren’t, some variables are capitalised, some aren’t. Makes it really hard to read, you’ll also probably find yourself trying to access a class when you meant to access a instantiated variable.

ThisIsAClass.ThisIsAFunction();
ThisIsAClass.thisIsAVariable;

is the traditional scheme.

1 Like

Sorry I didn’t describe it well. I try to do it like this.

Player → collider(TriggerObject) → Door(Object with a animation script) “collider and Door are a separate objects. And somewhere between them I got a null reference alsow collider is just a trigger it didn’t have any scripts (should I add ones?)

And here is the error.

NullReferenceException: Object reference not set to an instance of an object

DoorRayCast.Update () (at Assets/doors_active/DoorRayCast.cs:72)

This one appears when Inputing a keybutton.

Thanks for helping me again

you’ve provided a partial chunk of code, the error says the problem is on line 72, which line is that in the code you have pasted in?

The other parts have a same RayCast just for another object they work good.
But here the full code.

public class DoorRayCast : MonoBehaviour
{
    public float RayDistanse = 3f;
    private float PhoneRayDistance = 3f;
    private float CeilingDistance = 5f;
    public phoneAnimationScript myphoneAnimationScript;
    public phoneAnimationScript myCharacterAnimationScript;
    public CeilingDoorAnimCtrl CeilingDoorsCtrl;
   
    Camera CameraMain;
   
   public void Start()
    {
        CameraMain = GetComponent<Camera>();
    }
    public void Update()
    {       
        RaycastHit hit;
        Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        if (Physics.Raycast(ray, out hit, RayDistanse))
        {        
                if (hit.transform.tag == "DoorTag")
                { 
                   
                    if (Input.GetButtonDown("actButton"))
                    {
                        hit.collider.GetComponent<DoorsActAnim>().DoorTrigger();
                    }
                   
                }
            }
        RaycastHit closedHit;
        Ray closedRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        if (Physics.Raycast(closedRay, out closedHit, RayDistanse))
        {
            if (hit.transform.tag == "ClosedDoor")
            {
                if (Input.GetButtonDown("actButton"))
                {
                    hit.collider.GetComponent<ClosedDoor>().DoorTriggerClosed();
                }
            }
        }
        RaycastHit cellPhone;
        Ray phoneRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        if (Physics.Raycast(phoneRay, out cellPhone, PhoneRayDistance))
        {
            if (hit.transform.gameObject.tag == "Phone")
            {
                if (Input.GetButtonDown("actButton"))
                {
                    phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
                    StartCoroutine(myphoneAnimationScript.CutSceneStart());
                    StartCoroutine(myCharacterAnimationScript.CutSceneStart());
                }
                //Debug.Log("RayHitPhone");
            }
        }
        RaycastHit CeilingButton;
        Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
            if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
            {
                if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
                {
                   
                    if (Input.GetButtonDown("actButton"))
                    {
                        CeilingDoorAnimCtrl cas = GetComponent<CeilingDoorAnimCtrl>();
                        CeilingButton.transform.gameObject.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
                    }
                    Debug.Log("hitCollider");
                }
            }
        }
    }

and you’ve missed off however many lines of “using xxxx” from the top of your code chunk so we still cannot see what line the error it referring to… anyway, if it’s line 69 in the chunk you’ve just pasted (2 usings and a blank space missing), it would appear the thing that was hit doesn’t have a “CeilingDoorAnimCtrl” script attached to it.

on a design note, you appear to be doing things “backwards”, 4 raycasts each frame to check for each thing, all of which only do something when a button is pushed :face_with_spiral_eyes:

check for the button push,
raycast to see what was hit,
do something based on the thing hit.

would make a lot more sense and be event driven (the button push) rather than repeating every frame. Raycasts might not be the biggest sin per frame but it’s still a odd approach :slight_smile:

edit: and if you want to get super snazzy you could hand off the “do something” to the object that was hit so it’s

check for button push,
raycast to hit,
tell hit to “do it’s thing”

rather than doing everything in this script “remotely”

1 Like

I made it thank to give me an idea. I just create a relocate script for a separate object.

Have to use 3 scripts now but it work.

Raycast

        RaycastHit CeilingButton;
        Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
            if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
            {
                if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
                {
                   
                    if (Input.GetButtonDown("actButton"))
                    {
                        RelocateScript pas = GetComponent<RelocateScript>();
                        CeilingButton.collider.GetComponent<RelocateScript>().Relocate();
                    }
                    Debug.Log("hitCollider");
                }
            }
        }
    }

Then Relocate Script on a seperate collider

public class RelocateScript : MonoBehaviour {
    public CeilingDoorAnimCtrl CellDoorAnim;
    public void Relocate ()
    {
        CeilingDoorAnimCtrl pas = GetComponent<CeilingDoorAnimCtrl>();
        CellDoorAnim.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
    }
}

And Then Animation

public class CeilingDoorAnimCtrl : MonoBehaviour {
    private Animator CeilingAnimator;
    static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
    static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
    // Use this for initialization
    void Start () {
        CeilingAnimator = GetComponent<Animator>();
    }
   
    public void OpenDoors()
    {
        CeilingAnimator.SetBool("Open", true);
    }
    public void ClosedDoors()
    {
        CeilingAnimator.SetBool("Close", true);
    }
}

I understand you I will make my code cleaner in the future. Thanks