Varialbes on prefab script always null
I have a menu that you can select a weapon from. I assigned some weapon prefabs in a list in the inspector - List<Weapon> weapons;
- ( I used Weapon instead of GameObject since then I wouldn't have to attack the performance with GetComponent() ) I am very confused. All the values are assigned on the prefab, but when I try to access them through another script ( The menu script ) it always returns null. It is probably something stupid I have done :) The thing is, I can access variables in the Weapon
class, but not in the PlayerWeapon
class. PlayerWeapon
is accessed through Weapon.weaponInfo
Scipts:
using UnityEngine;
[System.Serializable]
public class Weapon : MonoBehaviour {
public ParticleSystem muzzleFlash;
public GameObject hitEffectPrefab;
public Sprite weaponIcon;
public GearType gearType;
public PlayerWeapon weaponInfo;
}
using UnityEngine;
[System.Serializable]
public class PlayerWeapon {
public string name = "My Weapon";
public int damage;
public int ammo;
public int maxAmmo;
public float additionalReloadTime;
public float range;
public float fireRate;
public float scopedFOV;
public float kickback;
public Vector3 offsetPos;
public Quaternion offsetRot;
public Sprite scopedOverlay;
public GameObject graphics;
public AudioClip shootSound;
public AudioClip reloadSound;
public PlayerWeapon()
{
damage = 10;
maxAmmo = 30;
ammo = maxAmmo;
additionalReloadTime = .2f;
range = 100f;
fireRate = 10f;
scopedFOV = 20f;
kickback = 1f;
}
}
using System.Collections.Generic;
using UnityEngine;
public class GearMenu : MonoBehaviour {
public SinglePlayerManager manager;
public GearSlot primaryWeaponSlot;
public Transform primaryWeaponSelect;
public GameObject gearItem;
public List<Weapon> primaryWeapons;
void Start()
{
SelectGear(primaryWeapons[0], GearType.Primary);
}
public void SelectPrimary()
{
primaryWeaponSelect.gameObject.SetActive(true);
foreach(Transform child in primaryWeaponSelect)
{
Destroy(child.gameObject);
}
foreach (Weapon weapon in primaryWeapons)
{
GameObject itemGO = Instantiate(gearItem, primaryWeaponSelect);
GearItem item = itemGO.GetComponent<GearItem>();
item.SetItem(weapon.weaponInfo.name, weapon.weaponIcon, weapon.weaponInfo.graphics, weapon.gearType, this);
}
}
public void SelectGear(Weapon _weaponPrefab, GearType _type)
{
if (_type == GearType.Primary)
{
Weapon _weapon = _weaponPrefab;
primaryWeaponSlot.SetGear(_weapon.weaponIcon, _weapon.weaponInfo.graphics);
primaryWeaponSelect.gameObject.SetActive(false);
}
}
public void Spawn()
{manager.SpawnPlayer(primaryWeaponSlot.gearPrefab.GetComponent<Weapon>());
}
}
public enum GearType
{
Primary,
Secondary,
Grenade
}
well then the problem is with playerweapon, can you share the inspector of the playerweapon at runtime (for checking the weaponinfo value)? since the code seems fine i cant see any errors
sorry but your error makes no sense :( can you before trying to access any of the weapon in the foreach print all their weapon.name (not the weaponinfo.name) just in case there is one lost object with the weapon script without graphics object? and check if the error ocurrs when trying to access any of the weapons or just 1? also try accessing the weaponInfo.damage rather than the graphic in case the graphic is being destroyed at runtime or something like that?
I can access all of the properties of the Weapon
class, but not the PlayerWeapon
(weaponInfo), like weapon.name, weapon.gameObject. I can't access thing like weaponInfo.weaponIcon, weaponInfo.name etc.
Your answer
Follow this Question
Related Questions
NullRefrenceException - Cant find the cost 1 Answer
How to access a variable from another C# script in Unity 1 Answer
Help with converting small pseudo script to C# please? 0 Answers
C# Mouselook Script errors, need assistance. 1 Answer
NullReferenceException: Object reference not set to an instance of an object, again 2 Answers