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

Question Having a bit of trouple with referencing a public variable in one script from another script

Discussion in 'Editor & General Support' started by ZTheCdr, Jun 15, 2020.

  1. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    So I have the following code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class JumpPowerup : MonoBehaviour
    6. {
    7.     public GameObject pickupEffect;
    8.     public MovePlayer movePlayer;
    9.  
    10.     void OnTriggerEnter(Collider other) {
    11.         if (other.CompareTag("Player")) { //then the player is in contact with the powerup
    12.             Pickup(other);
    13.         }
    14.     }
    15.  
    16.     private void Pickup(Collider player) {
    17.  
    18.         //do an effect
    19.         Instantiate(pickupEffect, transform.position, transform.rotation);
    20.  
    21.         //give the player more jump height by modifying the jumpForce public variable in the MovePlayer script that sits on the Player GameObject
    22.  
    23.         player.GetComponent<MovePlayer>().jumpForce = new Vector3(0, 20, 0); // this does not work
    24.      
    25.         movePlayer.jumpForce = new Vector3(0, 20, 0); //this does work: why?
    26.  
    27.         //gets rid of the powerup
    28.         Destroy(gameObject);
    29.     }
    30. }
    31.  
    Why does player.GetComponent<MovePlayer>().jumpForce = new Vector3(0, 20, 0); not work (it gives the error "NullReferenceException: Object reference not set to an instance of an object") while the line below it does and is successfully able to change jumpForce? It is my (clearly incorrect) understanding that these two lines of code would do the same thing.

    Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    player.GetComponent<MovePlayer>() will return null if the object you collided with does not have a MovePlayer component. Are you 100% sure the object you collided with has that component? Try logging some info:

    Code (CSharp):
    1. MovePlayer movePlayerFromCollider = player.GetComponent<MovePlayer>();
    2.  
    3. Debug.Log($"The name of the object we collided with is {player.gameObject.name}. That object has a MovePlayer component: {movePlayerFromCollider != null}");
    If that prints false, there is no MovePlayer component on the GameObject you collided with.
     
  3. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    Aha! I know the problem now. My character is a snowman, and it is made up of 3 different objects each with there own colliders (head, middle, and bottom). The player GameObject was an empty game object that I created to hold these three things and also govern the movement and physics for the player (it has a rigid body component and the movePlayer script). So, the power up is colliding with one of these parts of the player, which does not have the script on it as the script sits the whole thing, not on any one of the individual components.

    Anyway around this? Is there a way to acess the parent of the object that is colliding?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You could use
    GetComponentInParent<MovePlayer>()