Search Unity

Problems with scripting, Help!

Discussion in 'Scripting' started by HarmlessRaccon, Feb 18, 2020.

  1. HarmlessRaccon

    HarmlessRaccon

    Joined:
    Mar 22, 2018
    Posts:
    7
    Hello everyone!

    I am making a game in which you control a ball that moves up, down, left and right and shoots bullets at incoming enemies. The problem is: I'm trying to not only destroy the enemy object, but also regenerate 1 bullet upon collision with the enemy, but even after trying many ways of detecting the object it always gives me the nullreferenceexception error. The debug successfully informs me that my bullet collided with "Enemy" or "Enemy (Clone)" depending on the occasion, yet after trying gameObject.name, gameObject.tag and compareTag it still does not work. here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletScript : MonoBehaviour
    5. {
    6.     public Rigidbody2D rb;
    7.     public float moveSpeed = 9;
    8.     Vector2 movement;
    9.     public GameObject bullet;
    10.     playerMovement regen;
    11.     public GameObject Enemy;
    12.     void FixedUpdate()
    13.     {
    14.         movement.x = 1;
    15.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    16.         StartCoroutine(destroy());
    17.     }
    18.     void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         Debug.Log("Collision with: " + collision.gameObject.name);
    21.         regen = GetComponent<playerMovement>();
    22.         float regenAmmo = regen.bulletAmmo;
    23.         GameObject enemy = GameObject.Find("Enemy");
    24.         GameObject enemy1 = GameObject.Find("Enemy (Clone)");
    25.         if (collision.gameObject == enemy || collision.gameObject == enemy1)
    26.         {
    27.             regenAmmo++;
    28.             Destroy(Enemy);
    29.         }
    30.     }
    31.     IEnumerator destroy()
    32.     {
    33.         yield return new WaitForSeconds(3);
    34.         Destroy(bullet);
    35.     }
    36. }
    37.  
    Here is the "bulletAmmo" float i'm accesing

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class playerMovement : MonoBehaviour
    5. {
    6.     public float moveSpeed = 5f;
    7.     public Rigidbody2D rb;
    8.     Vector2 movement;
    9.     public GameObject Bullet;
    10.     public GameObject Player;
    11.     bool shootNow;
    12.     Vector3 position;
    13.     public float bulletAmmo = 3;
    14.     void Update()
    15.     {
    16.         if (bulletAmmo > 3)
    17.         {
    18.             bulletAmmo--;
    19.         }
    20.         if (bulletAmmo <= 0)
    21.         {
    22.             destroy();
    23.         }
    24.         movement.x = Input.GetAxisRaw("Horizontal");
    25.         movement.y = Input.GetAxisRaw("Vertical");
    26.         float Playerx = Player.transform.position.x;
    27.         float Playery = Player.transform.position.y;
    28.         position = new Vector3(Playerx+1, Playery, 0);
    29.         shootNow = Input.GetKeyDown("q");
    30.         if (shootNow == true)
    31.         {
    32.             Object.Instantiate(Bullet, position, Quaternion.identity);
    33.             bulletAmmo--;
    34.         }
    35.     }
    36.     void FixedUpdate()
    37.     {
    38.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    39.     }
    40.     private void destroy()
    41.     {
    42.         Destroy(Player);
    43.     }
    44. }
    it was line 27, but i fixed it by instead of assigning it to a variable i just used regen.bulletAmmo EDIT: This did not actually fix it, why does line 27 give this error?. It was also line 29, where ciorbyn was right and taking out the space fixed it. But now, even without errors, it still doesnt regen my ammo nor does it destroy the enemy gameobject, am i missing something?

    After debugging, it seems the "if" statement IS reached
     
    Last edited: Feb 18, 2020
  2. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    Could you add a video so i could understand the problem very well
     
  3. HarmlessRaccon

    HarmlessRaccon

    Joined:
    Mar 22, 2018
    Posts:
    7
    Ok, Here it is: https://vimeo.com/user108753595/review/392228939/da3be2fde2

    Also it's line now line 30, not 29 or 27, maybe the program is stopping because it finds the error and doesnt execute the destroy command?

    if that video doesnt work, try this:
     
    Last edited: Feb 18, 2020