In this FPS-survival game I’m working on, the player is holding a flashlight. Pressing the ‘G’ key will “drop it on the floor” by instantiating a different flashlight prefab and Destroying the flashlight in his hand. My main goal here is to have the flashlight on the floor remember the float value for the battery-level, in case the player picks his flashlight back up.
Let’s say my flashlight battery level is at 0.5, (in this case I’ve overridden the value on line 9 to 0.5f to simplify) …Everything works as it should until the Unity-Inspector shows the public float value in the flashlight on the floor to be 1.0f.
I can’t figure out why, and it’s driving me crazy!
P.S. please forgive this poorly typed post, I haven’t slept in 2 days.![]()
Function to drop flashlight:
void DropItemInHand(){
GameObject NewDroppedFlashlight =
(GameObject)Instantiate (DroppedFlashlight,
gameObject.transform.position, gameObject.transform.rotation);
// Pass 0.5f to the "setter" of the instantiated prefab
NewDroppedFlashlight.GetComponent<FlashlightPickUp>().SetBatteryLevel(0.5f);
// destroy flashlight in hand
Destroy (LocationFlashlight);
HaveFlashlight = false;
}
FlashlightPickUp.cs
using UnityEngine;
using System.Collections;
public class FlashlightPickUp : MonoBehaviour
{
public string InteractionAvailable;
public float BatteryLevel;
void Start () {
BatteryLevel = 1.0f;
InteractionAvailable = "Press 'E' to pick up flashlight";
}
public void PickUp () {
Destroy (gameObject);
}
public void SetBatteryLevel(float b) {
BatteryLevel = b;
}
public float GetBatteryLevel() {
return BatteryLevel;
}
}