Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Error: Object reference not set to an instance of an object. Something to do with GetComponent

Discussion in 'Scripting' started by PanayiotisPetsas, Jun 22, 2018.

  1. PanayiotisPetsas

    PanayiotisPetsas

    Joined:
    May 28, 2018
    Posts:
    4
    Hi all, I'm new to unity and coding. I'm trying to make this game that based if the player is looking up, down, left or right he will shoot at that direction according to the animation. (It's a top-down 2D game) Here's the script for the animation: (which is also the movements as well)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     public float moreSpeed;
    7.     private float speed = 0.02f;
    8.     public Animator anim;
    9.  
    10.     public bool upAnim;
    11.     public bool downAnim;
    12.     public bool leftAnim;
    13.     public bool rightAnim;
    14.  
    15.     public void MoreMovement()
    16.     {
    17.         //                          --------------- Extra Speed Movements ----------------
    18.  
    19.  
    20.         if (Input.GetKey(KeyCode.UpArrow) && (Input.GetKey(KeyCode.Space)))
    21.         {
    22.             transform.Translate(0,speed + moreSpeed, 0);
    23.         }
    24.         if (Input.GetKey(KeyCode.DownArrow) && (Input.GetKey(KeyCode.Space)))
    25.         {
    26.             transform.Translate(0,-speed - moreSpeed, 0);
    27.         }
    28.         if (Input.GetKey(KeyCode.RightArrow) && (Input.GetKey(KeyCode.Space)))
    29.         {
    30.             transform.Translate(speed + moreSpeed, 0, 0);
    31.         }
    32.         if (Input.GetKey(KeyCode.LeftArrow) && (Input.GetKey(KeyCode.Space)))
    33.         {
    34.             transform.Translate(-speed - moreSpeed, 0, 0);
    35.         }
    36.     }
    37.     public void Movement() {
    38.         //                        ---------------Normal Movements---------------
    39.         if (Input.GetKey(KeyCode.UpArrow))
    40.         {
    41.             //  xUp = xUp + speed;
    42.             transform.Translate(0, speed, 0);
    43.             anim.SetBool("Up", true);
    44.             anim.SetBool("Down", false);
    45.             anim.SetBool("Left", false);
    46.             anim.SetBool("Right", false);
    47.             upAnim = true;
    48.             downAnim = false;
    49.             leftAnim = false;
    50.             rightAnim = false;
    51.         }
    52.         if (Input.GetKey(KeyCode.DownArrow))
    53.         {
    54.             //  xDown = xDown - speed;
    55.             transform.Translate(0, -speed, 0);
    56.             anim.SetBool("Up", false);
    57.             anim.SetBool("Down", true);
    58.             anim.SetBool("Left", false);
    59.             anim.SetBool("Right", false);
    60.             upAnim = false;
    61.             downAnim = true;
    62.             leftAnim = false;
    63.             rightAnim = false;
    64.         }
    65.         if (Input.GetKey(KeyCode.RightArrow))
    66.         {
    67.             //  yUp = yUp + speed;
    68.             transform.Translate(speed, 0, 0);
    69.             anim.SetBool("Up", false);
    70.             anim.SetBool("Down", false);
    71.             anim.SetBool("Left", false);
    72.             anim.SetBool("Right", true);
    73.             upAnim = false;
    74.             downAnim = false;
    75.             leftAnim = false;
    76.             rightAnim = true;
    77.         }
    78.         if (Input.GetKey(KeyCode.LeftArrow))
    79.         {
    80.             //  yDown = yDown - speed;
    81.             transform.Translate(-speed, 0, 0);
    82.             anim.SetBool("Up", false);
    83.             anim.SetBool("Down", false);
    84.             anim.SetBool("Left", true);
    85.             anim.SetBool("Right", false);
    86.             upAnim = false;
    87.             downAnim = false;
    88.             leftAnim = true;
    89.             rightAnim = false;
    90.         }
    91.     }
    92.  
    93.     void Start () {
    94.         anim = GetComponent<Animator>();
    95.     }
    96.  
    97.     private void FixedUpdate()
    98.     {
    99.         //we always use FixedUpdate for physics (rigidbody2d)
    100.         {
    101.  
    102.             Movement();
    103.             MoreMovement();
    104.        
    105.         }
    106.     }
    107.     // Update is called once per frame
    108.     void Update () {
    109.  
    110.     }
    111. }
    112.  


    Based if the player is looking up, down, left or right, an appropriate bool will be set to true, while the others to false. Now I'll wanna use getcomponent on my prefab beam1 (which is the beam which is being shot) so get the information on the PlayerMovement scripts to the prefab script. But it doesn't quite work.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BeamPrefabShooting : MonoBehaviour {
    6.  
    7.     public Rigidbody2D rb;
    8.     public float bulletSpeed;
    9.     public GameObject collidedBullet;
    10.     private PlayerMovement playerMovement;
    11.     public GameObject beam1;
    12.     private void Start()
    13.     {
    14.         playerMovement = beam1.GetComponent<PlayerMovement>();
    15.     }
    16.     void FixedUpdate () {
    17.         if (playerMovement.rightAnim == true)
    18.         {
    19.             rb.velocity = new Vector2(bulletSpeed, 0);
    20.         }
    21.         if (playerMovement.leftAnim == true)
    22.         {
    23.             rb.velocity = new Vector2(-bulletSpeed, 0);
    24.         }
    25.         if (playerMovement.upAnim == true)
    26.         {
    27.             rb.velocity = new Vector2(0, bulletSpeed);
    28.         }
    29.         if (playerMovement.downAnim == true)
    30.         {
    31.             rb.velocity = new Vector2(0, -bulletSpeed);
    32.         }
    33.     }
    34.     private void OnTriggerEnter2D(Collider2D collision)
    35.     {
    36.         if (collision.gameObject.tag == "wall")
    37.         {
    38.             Destroy(collidedBullet);
    39.         }
    40.     }
    41. }
    42.  
    The beam can only be shot if the player has picked up the beam from the ground (if the gameobject has been destroyed)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BeamShooting : MonoBehaviour
    6. {
    7.     public GameObject destroyedBeam;
    8.     public GameObject beam;
    9.     public Transform bulletSpawn;
    10.  
    11.     void Update()
    12.     {
    13.         BeamShoot();
    14.     }
    15.  
    16.     public void BeamShoot()
    17.     {
    18.         if (destroyedBeam = destroyedBeam)
    19.         {
    20.         }
    21.         else
    22.         {
    23.             if (Input.GetKeyDown(KeyCode.E))
    24.             {
    25.                 Instantiate(beam, bulletSpawn.transform);
    26.             }
    27.         }
    28.  
    29.     }
    30.  
    31. }
    32.  
    Here's the pickup script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BeamPickUp : MonoBehaviour {
    6.    public Object Player;
    7.    public Object pickUpBeam;
    8.     public int itemCount;
    9.     // Use this for initialization
    10.     void Start () {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         afterDestroyed();
    17.  
    18.     }
    19.     void OnCollisionEnter2D(Collision2D Player)
    20.     {
    21.         if (Player.gameObject.tag == "Beam" )
    22.         {
    23.             Destroy(pickUpBeam);
    24.             itemCount++;
    25.             print("Item count = " + itemCount);
    26.         }
    27.     }
    28.  
    29.     void afterDestroyed()
    30.     {
    31.      /*   if (pickUpBeam = pickUpBeam)
    32.         {
    33.             print("not destroyed");
    34.         }
    35.         else
    36.         {
    37.             print("destroyed");
    38.         }
    39.  
    40.     */}
    41. }
    42.  

    Thank you so much. I'm certain that the error is about unity not being able to correctly transfer the playermoevment script to the prefab script but I added the rest of the scripts that are related just in case. I know it's a bit of a mess, but I'm slowly learning and correcting everything as well on the way.

    Thank you!!!

    EDIT: This is the error I get:
    NullReferenceException: Object reference not set to an instance of an object
    BeamPrefabShooting.FixedUpdate () (at Assets/BeamPrefabShooting.cs:17)
     
    Last edited: Jun 22, 2018
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    The error message will have the script and codeline that caused it at the end. Can you post the error message in full.
     
  3. PanayiotisPetsas

    PanayiotisPetsas

    Joined:
    May 28, 2018
    Posts:
    4
    NullReferenceException: Object reference not set to an instance of an object
    BeamPrefabShooting.FixedUpdate () (at Assets/BeamPrefabShooting.cs:22)


    It used to be on cs:17 but after messing around it's now on 22. Not sure even why but it still doesn't work. The beam wont shoot and will stay next to the player. If I remove all the if statements the beam will shoot normally in the same direction as the player (always on the right though)

     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. public class BeamPrefabShooting : MonoBehaviour {
    3.     public Rigidbody2D rb;
    4.     //<.snip.>
    5.     void FixedUpdate () {
    6.         //<.snip.>
    7.             rb.velocity = new Vector2(bulletSpeed, 0);
    8.        
    9.  
    You've not set rb to anything, then you try to access it's "velocity" property. Have you put the rigidbody in the inspector slot?
     
  5. PanayiotisPetsas

    PanayiotisPetsas

    Joined:
    May 28, 2018
    Posts:
    4
    I've set the rigidbody to the prefab itself.


    Thanks for your time so far. :)


    EDIT: This is the error I get:
    NullReferenceException: Object reference not set to an instance of an object
    BeamPrefabShooting.FixedUpdate () (at Assets/BeamPrefabShooting.cs:17)