Search Unity

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

Discussion in 'Getting Started' started by MikeTeavee, Jan 9, 2016.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    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.:confused:


    Function to drop flashlight:
    Code (CSharp):
    1.  
    2.     void DropItemInHand(){
    3.  
    4.         GameObject NewDroppedFlashlight =
    5.                   (GameObject)Instantiate (DroppedFlashlight,  
    6.                              gameObject.transform.position, gameObject.transform.rotation);
    7.  
    8.         // Pass 0.5f to the "setter" of the instantiated prefab
    9.        NewDroppedFlashlight.GetComponent<FlashlightPickUp>().SetBatteryLevel(0.5f);
    10.         // destroy flashlight in hand
    11.         Destroy (LocationFlashlight);
    12.         HaveFlashlight = false;
    13.     }
    FlashlightPickUp.cs
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class FlashlightPickUp : MonoBehaviour
    8.  
    9. {
    10.  
    11.     public string InteractionAvailable;
    12.  
    13.     public float BatteryLevel;
    14.  
    15.    
    16.  
    17.     void Start () {
    18.         BatteryLevel = 1.0f;
    19.  
    20.         InteractionAvailable = "Press 'E' to pick up flashlight";
    21.  
    22.     }
    23.  
    24.     public void PickUp () {
    25.         Destroy (gameObject);
    26.     }
    27.     public void SetBatteryLevel(float b) {
    28.         BatteryLevel = b;
    29.     }
    30.     public float GetBatteryLevel() {
    31.         return BatteryLevel;
    32.     }
    33. }
     
    Last edited: Jan 10, 2016
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
    MikeTeavee likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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:
    Code (CSharp):
    1. public class FlashlightPickUp : MonoBehaviour
    2. {
    3.     public string InteractionAvailable = "Press 'E' to pick up flashlight";
    4.     public float BatteryLevel = 1;
    5.  
    6.     void Start () {
    7.         // don't override public property values here!
    8.     }
    9.  
     
    jhocking, tedthebug and MikeTeavee like this.
  4. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    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.
     
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    good spot JoeS, that bug would have had me tearing my hair out
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
  7. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    I just printed this list and taped it to my wall.
     
    JoeStrout likes this.