,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
Your answer
![](https://koobas.hobune.stream/wayback/20220612144037im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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