Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing component on another Object.

Discussion in 'Scripting' started by Harardin, Aug 18, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    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
    Code (CSharp):
    1.  
    2.     private float CeilingDistance = 5f;
    3.     public phoneAnimationScript myphoneAnimationScript;
    4.     public phoneAnimationScript myCharacterAnimationScript;
    5.     public CeilingDoorAnimCtrl CeilingDoorsCtrl;
    6.    
    7.     Camera CameraMain;
    8.    
    9.    public void Start()
    10.     {
    11.         CameraMain = GetComponent<Camera>();
    12.     }
    13.     public void Update()
    14.     {
    15. RaycastHit CeilingButton;
    16.         Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    17.             if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
    18.             {
    19.                 if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
    20.                 {
    21.                    
    22.                     if (Input.GetButtonDown("actButton"))
    23.                     {
    24.                         CeilingDoorAnimCtrl cas = GetComponent<CeilingDoorAnimCtrl>();
    25.                         CeilingButton.collider.gameObject.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
    26.                     }
    27.                     Debug.Log("hitCollider");
    28.                 }
    29.             }
    30.         }
    31.     }
    And the Animation Code

    Code (CSharp):
    1. public class CeilingDoorAnimCtrl : MonoBehaviour {
    2.     private Animator CeilingAnimator;
    3.     static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
    4.     static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
    5.     // Use this for initialization
    6.     void Start () {
    7.         CeilingAnimator = GetComponent<Animator>();
    8.     }
    9.    
    10.     public void OpenDoors()
    11.     {
    12.         CeilingAnimator.SetBool("Open", true);
    13.     }
    14.     public void ClosedDoors()
    15.     {
    16.         CeilingAnimator.SetBool("Close", true);
    17.     }
    18. }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Please include the exact error with the line it's occurring on.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
    Kiwasi likes this.
  4. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    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
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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?
     
  6. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    The other parts have a same RayCast just for another object they work good.
    But here the full code.
    Code (CSharp):
    1. public class DoorRayCast : MonoBehaviour
    2. {
    3.     public float RayDistanse = 3f;
    4.     private float PhoneRayDistance = 3f;
    5.     private float CeilingDistance = 5f;
    6.     public phoneAnimationScript myphoneAnimationScript;
    7.     public phoneAnimationScript myCharacterAnimationScript;
    8.     public CeilingDoorAnimCtrl CeilingDoorsCtrl;
    9.    
    10.     Camera CameraMain;
    11.    
    12.    public void Start()
    13.     {
    14.         CameraMain = GetComponent<Camera>();
    15.     }
    16.     public void Update()
    17.     {      
    18.         RaycastHit hit;
    19.         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    20.         if (Physics.Raycast(ray, out hit, RayDistanse))
    21.         {        
    22.                 if (hit.transform.tag == "DoorTag")
    23.                 {
    24.                    
    25.                     if (Input.GetButtonDown("actButton"))
    26.                     {
    27.                         hit.collider.GetComponent<DoorsActAnim>().DoorTrigger();
    28.                     }
    29.                    
    30.                 }
    31.             }
    32.         RaycastHit closedHit;
    33.         Ray closedRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    34.         if (Physics.Raycast(closedRay, out closedHit, RayDistanse))
    35.         {
    36.             if (hit.transform.tag == "ClosedDoor")
    37.             {
    38.                 if (Input.GetButtonDown("actButton"))
    39.                 {
    40.                     hit.collider.GetComponent<ClosedDoor>().DoorTriggerClosed();
    41.                 }
    42.             }
    43.         }
    44.         RaycastHit cellPhone;
    45.         Ray phoneRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    46.         if (Physics.Raycast(phoneRay, out cellPhone, PhoneRayDistance))
    47.         {
    48.             if (hit.transform.gameObject.tag == "Phone")
    49.             {
    50.                 if (Input.GetButtonDown("actButton"))
    51.                 {
    52.                     phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
    53.                     StartCoroutine(myphoneAnimationScript.CutSceneStart());
    54.                     StartCoroutine(myCharacterAnimationScript.CutSceneStart());
    55.                 }
    56.                 //Debug.Log("RayHitPhone");
    57.             }
    58.         }
    59.         RaycastHit CeilingButton;
    60.         Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    61.             if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
    62.             {
    63.                 if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
    64.                 {
    65.                    
    66.                     if (Input.GetButtonDown("actButton"))
    67.                     {
    68.                         CeilingDoorAnimCtrl cas = GetComponent<CeilingDoorAnimCtrl>();
    69.                         CeilingButton.transform.gameObject.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
    70.                     }
    71.                     Debug.Log("hitCollider");
    72.                 }
    73.             }
    74.         }
    75.     }
    76.  
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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 :confused:

    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 :)


    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"
     
    Kiwasi likes this.
  8. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    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

    Code (CSharp):
    1.         RaycastHit CeilingButton;
    2.         Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    3.             if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
    4.             {
    5.                 if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
    6.                 {
    7.                    
    8.                     if (Input.GetButtonDown("actButton"))
    9.                     {
    10.                         RelocateScript pas = GetComponent<RelocateScript>();
    11.                         CeilingButton.collider.GetComponent<RelocateScript>().Relocate();
    12.                     }
    13.                     Debug.Log("hitCollider");
    14.                 }
    15.             }
    16.         }
    17.     }
    Then Relocate Script on a seperate collider

    Code (CSharp):
    1. public class RelocateScript : MonoBehaviour {
    2.     public CeilingDoorAnimCtrl CellDoorAnim;
    3.     public void Relocate ()
    4.     {
    5.         CeilingDoorAnimCtrl pas = GetComponent<CeilingDoorAnimCtrl>();
    6.         CellDoorAnim.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
    7.     }
    8. }
    And Then Animation

    Code (CSharp):
    1. public class CeilingDoorAnimCtrl : MonoBehaviour {
    2.     private Animator CeilingAnimator;
    3.     static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
    4.     static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
    5.     // Use this for initialization
    6.     void Start () {
    7.         CeilingAnimator = GetComponent<Animator>();
    8.     }
    9.    
    10.     public void OpenDoors()
    11.     {
    12.         CeilingAnimator.SetBool("Open", true);
    13.     }
    14.     public void ClosedDoors()
    15.     {
    16.         CeilingAnimator.SetBool("Close", true);
    17.     }
    18. }
    19.  
     
  9. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    I understand you I will make my code cleaner in the future. Thanks