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 DarkC0der · Dec 05, 2017 at 08:57 PM · unity 5script.oop

,Why do I get the same value from object property?

,Hi everyone, I am stuck with a simple problem in my weapon script.

I have a non monobehaviour derived class where I set the [System.Serializable] attribute to make it appear in the inspector, but when I use this class in another and try to get my float value it always returns same value even though I change variable in inspector and the default values in the object itself.

Here Is the code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class BulletSpreading
 {
     public float minSpread = 0f;
     public float maxSpread = 0f;
 
     public float shootSpreadIncrease = 0f;
     public float spreadNormalizeTime = 0f;
 
     [HideInInspector]
     public float currentSpread = 0f;
 
     public BulletSpreading()
     {
         currentSpread = minSpread;
     }
 }
 
 [System.Serializable]
 public class ShootingData
 {
     public float fireRate = 0.1f;         
     public float reloadTime = 1.5f;  
 
     public int totalBullets = 50;    
     public int magazineCapacity = 5;    
     public int bulletsPerShoot = 1;
 
     //[HideInInspector] 
     public float fireTimer = 0f;
     //[HideInInspector] 
     public int currentBulletsCount = 0; 
 
     public bool IsMagazineEmpty { get { return currentBulletsCount == 0; } }
     public bool IsMagazineFull { get { return currentBulletsCount == magazineCapacity; } }
     public bool IsOutOfBullets { get { return totalBullets == 0; } }
 
     public ShootingData()
     {
         currentBulletsCount = magazineCapacity;
     }
 }
 
 public class GunBase : MonoBehaviour {
 
     // Point from where bullets and particle effects are instantiated
     public Transform shootPoint;         
 
     [Header("Main gun settings")]
     public ShootingData shootingData = new ShootingData();
     public BulletSpreading bulletSpreading = new BulletSpreading();
 
     [Header("VFX")]
     public ParticleSystem muzzleFlash;
     public GameObject shootParticlePrefab;
 
     bool IsReloading = false;
 
     public virtual bool Shoot()
     {
         if (Time.time < shootingData.fireTimer)
             return false;
 
         if (shootingData.IsMagazineEmpty)
             return false;
 
         // Get the next time for fire
         shootingData.fireTimer = Time.time + shootingData.fireRate;
 
         // Decrease bullet count by per shoot amount
         shootingData.currentBulletsCount -= shootingData.bulletsPerShoot;
     
         // Shooting effects
         ShootEffects ();
 
         return true;
     }
 
     public void Reload()
     {
         if (shootingData.IsOutOfBullets || shootingData.IsMagazineFull || IsReloading)
             return;
 
         StartCoroutine (ReloadCoroutine ());
     }
 
     public void ShootEffects()
     {
         if (muzzleFlash != null)
             muzzleFlash.Play (true);
 
         if (shootParticlePrefab != null) {
             GameObject shootParticleInstance = Instantiate(shootParticlePrefab, shootPoint.position, shootPoint.rotation);
         }
     }
         
     IEnumerator ReloadCoroutine()
     {
         // Set IsReloading var to true
         IsReloading = true;
 
         yield return new WaitForSeconds (shootingData.reloadTime);
 
         // How many bullets needed to fully fill magazine
         int amountNeeded = shootingData.magazineCapacity - shootingData.currentBulletsCount;
 
         // Check if we have needed amount of bullets 
         if (shootingData.totalBullets >= amountNeeded) 
         {
             // If so just decrease total bullets by that amount 
             // and give current count a fresh full magazine
             shootingData.totalBullets -= amountNeeded;
             shootingData.currentBulletsCount = shootingData.magazineCapacity;
         }
         else 
         {
             // If don't have needed amount than just grab the left outs and add to current
             shootingData.currentBulletsCount += shootingData.totalBullets;
             shootingData.totalBullets = 0;
         }
 
         // Set IsReloading false
         IsReloading = false;
     }
 }

When I try to get bulletSpreading.currentSpread it gives me 2f always instead of 0. If anyone knows what is the problem please help. Sorry for my English though.

Comment
Add comment
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

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

191 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 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

How to create a "trap" when ground is tiled with collider. 0 Answers

Scripts Interfering With Each Other 1 Answer

I can not add an existing script 0 Answers

Map creator inside my game 0 Answers

Displaying Properties from Revit in Unity VR 0 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