Trouble changing variable in instantiated flashlight("pick-up-able")-prefab

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.:face_with_spiral_eyes:

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;
    }
}

You can’t just drop the flashlight in the hand & change its tag etc so it can be picked up again?

If not you could instantiate the one on the ground first & in the start function for the one on the ground access the other flashlight script to get & set the battery value then destroy the one in the hand.

1 Like

The issue is that the Start method fires later, on the next time through the event loop. This is after your call to SetBatteryLevel. And FlashlightPickUp’s Start method is resetting the BatteryLevel to 1. If you don’t want the BatteryLevel to always start at 1, then don’t do that!

You should instead put the default value for the BatteryLevel right on the declaration, so it looks like this:

public class FlashlightPickUp : MonoBehaviour
{
    public string InteractionAvailable = "Press 'E' to pick up flashlight";
    public float BatteryLevel = 1;
  
    void Start () {
        // don't override public property values here!
    }
3 Likes

Works now after initializing BatteryLevel to 1 right in the declaration. Which led me to thinking, “what if I set BatteryLevel in Awake() ?” …this also seems to work, but is it safe?

That is also good advice. Thanks.

good spot JoeS, that bug would have had me tearing my hair out

You’re thinking along productive lines. Yes, that’s safe. See the documented order of the magic methods — Awake is called right away, and Start is called much later.

I just printed this list and taped it to my wall.

1 Like