Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by RogueMacro · Apr 22, 2019 at 09:52 AM · c#unity 5nullreferenceexceptionprefabsvariables

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
 }
 






Prefab referenced at runtime

skjermbigdflde.png (34.5 kB)
Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Apr 22, 2019 at 11:36 AM 0
Share

you need to share your scripts we cant know

avatar image RogueMacro · Apr 22, 2019 at 01:26 PM 0
Share
 ThereYouGo(:D);
avatar image xxmariofer RogueMacro · Apr 22, 2019 at 03:14 PM 0
Share

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

avatar image xxmariofer RogueMacro · Apr 23, 2019 at 02:31 PM 0
Share

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?

avatar image RogueMacro xxmariofer · Apr 23, 2019 at 02:38 PM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

175 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges