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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Help with adding gameObjects to list

Discussion in 'Scripting' started by Corsair990, Mar 4, 2018.

  1. Corsair990

    Corsair990

    Joined:
    Jan 18, 2018
    Posts:
    7
    I am trying to add a gameObject to a list of weapons. I am getting a null reference exception. I want the player to walk into the trigger on the WeaponPickup and add the gameObject to the Weapons List.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.     public List<GameObject> Weapons = new List<GameObject>();
    8.  
    9.     void Start ()
    10.     {
    11.        
    12.     }
    13.  
    14.  
    15.     void Update ()
    16.     {
    17.      
    18.     }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponPickup : MonoBehaviour
    6. {
    7.     private float rotationSpeed = 1f;
    8.     private WeaponManager weaponMan;
    9.     private void Awake()
    10.     {
    11.         weaponMan = GetComponent<WeaponManager>();
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         RotateWeapon();
    17.     }
    18.  
    19.     void RotateWeapon()
    20.     {
    21.         Vector3 rotationVector = new Vector3(0, 1 * rotationSpeed, 0);
    22.         transform.Rotate(rotationVector);
    23.     }
    24.  
    25.     private void OnTriggerEnter(Collider other)
    26.     {
    27.         if (other.CompareTag("Player"))
    28.         {
    29.             weaponMan.Weapons.Add(gameObject);
    30.             Destroy(gameObject);
    31.         }
    32.     }
    33. }
     
    conuletv likes this.
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The weapon manager is probably not on the same game object as the weapon pickup, right?

    You must reference the correct game object and assign the value in code or through the inspector.

    That being said, you're adding the game object to a list, and also destroying it...
     
  3. Corsair990

    Corsair990

    Joined:
    Jan 18, 2018
    Posts:
    7
    That is correct it is on the player. Is there a way to reference the gameObject the WeaponPickup script is on without having to say something like public GameObject weapon; then setting weapon = GetComponent<GameObject>(); and draggin and dropping a prefab from the inspector.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not entirely sure what you just said, but you should make a reference to the weapon manager on the weapon pickup script.

    So, if your weapons are always in the game world, this is done easily by dragging n dropping. If they spawn into the world at various times, then one way to do it is to keep a reference to the weapon manager on the game object (script) that spawns them in. Then, pass it along to the script on the newly spawned game object (weapon).

    Also, regarding the last comment I made in my previous post, you can't destroy the game object and keep a reference to it at the same time.. I mean, you'd just be referencing a null value.
     
  5. Deleted User

    Deleted User

    Guest

    Yes there is. Assuming you only have the one instance of the WeaponManager in your scene at a time, it is possible to get it using the following code.

    Code (CSharp):
    1.     private WeaponManager weaponMan;
    2.     private void Awake()
    3.     {
    4.         weaponMan = FindObjectOfType<WeaponManager>();
    5.     }
     
  6. Corsair990

    Corsair990

    Joined:
    Jan 18, 2018
    Posts:
    7
    Thank you for the replies, I think I am not explaining this correctly. I have an object attached to the FPSController called Weapon Manager with the code above. I then have an object in the scene called WeaponPickup with the code above. The WeaponPickup has a box collider marked as a trigger. The WeaponPickup is a weapon prefab. When the player collides with the trigger. I want the exact Weapon prefab the FPSController collided with to be added to the WeaponManager list so that I can keep track of the Weapons the FPSController has collected. I guess I should scrap how I have this setup and come up with another solution.
     
  7. Deleted User

    Deleted User

    Guest

    So what it sounds like is you have a single class in the scene called WeaponManager which is attached to your player. Then you have placed weapons around the scene which call a trigger when the player walks into them. The trigger activates your WeaponPickUp script, which needs access to the WeaponManager to add it self to the list of weapons. If that's the case, the script that I provided would work.

    Code (CSharp):
    1. public class WeaponPickup : MonoBehaviour
    2. {
    3.     private float rotationSpeed = 1f;
    4.  
    5.     //Reference to the WeaponManager.
    6.     private WeaponManager weaponMan;
    7.     private void Awake()
    8.     {
    9.         //We don't know where the Manager is, so search the entire scene for it.  This will return the weaponManager on the fps controller.
    10.         weaponMan = FindObjectOfType<WeaponManager>();
    11.  
    12.         if(weaponMan == null){
    13.               Debug.LogError("Was unable to find the weapon Manager!");
    14.         }
    15.     }
    16.     void Update ()
    17.     {
    18.         RotateWeapon();
    19.     }
    20.     void RotateWeapon()
    21.     {
    22.         Vector3 rotationVector = new Vector3(0, 1 * rotationSpeed, 0);
    23.         transform.Rotate(rotationVector);
    24.     }
    25.     private void OnTriggerEnter(Collider other)
    26.     {
    27.         //Check to see if the collider is connected to the player.
    28.         if (other.transform.tag == "Player")
    29.         {
    30.             //Make sure the manager is not null.
    31.             if(weaponMan != null){
    32.                   //Add this weapon game object to the manager.
    33.                   weaponMan.Weapons.Add(gameObject);
    34.             }
    35.         }
    36.     }
    37. }
    You may notice that I have removed the Destroy(gameobject). Since you store a reference to the weapon object in the WeaponManager, if you destroy the object that the WeaponPickup is connected too after picking it up, the reference to the gameobject in WeaponManager will become null. After you pick up the weapon, it would be much better to change the weapons position to be out of the level, and also disable the rotation script. That way, the reference to the object in the WeaponManager will not be null.
     
    Corsair990 likes this.