Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not sure why my image isn't getting the current energy points value from player script

Discussion in '2D' started by DiamondEyes, May 21, 2018.

  1. DiamondEyes

    DiamondEyes

    Joined:
    May 20, 2018
    Posts:
    3
    I'm trying to get an image to change animations whenever the player's energy points is 5 or less, but it doesn't seem to be able to detect the changes in the point number. I've checked in the inspector to make sure it's actually changing properly, so basically it only works when I manually change the number in the inspector.

    here's the code I attached to the image:


    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7. using UnityEngine.UI;
    8.  
    9. public class Image : MonoBehaviour
    10. {
    11.  
    12.     private GameObject HealthImage;
    13.  
    14.     private GameObject EnergyImage;
    15.  
    16.     public GameObject Player;
    17.  
    18.     public int energyPoints;
    19.  
    20.     private Animator animator;
    21.  
    22.     //PlayerPlatformerController player;
    23.  
    24.     // Use this for initialization
    25.  
    26.     void Start()
    27.     {
    28.         animator = GetComponent<Animator>();
    29.         HealthImage = GameObject.Find("HealthImage");
    30.  
    31.         EnergyImage = GameObject.Find("EnergyImage");
    32.  
    33.         Player = GameObject.Find("Player");
    34.  
    35.         energyPoints = Player.GetComponent<PlayerPlatformerController>().energyPoints;
    36.  
    37.     }
    38.  
    39.     // Update is called once per frame
    40.  
    41.     void Update () {
    42. if (energyPoints <= 5)
    43.  
    44. {
    45.  
    46. animator.SetTrigger("LowEnergy");
    47.  
    48. }
    49.  
    50. else if (energyPoints >= 6)
    51.  
    52. {
    53.  
    54. animator.SetTrigger("NormalEnergy");
    55.  
    56. }
    57.  
    58. }
    59.  
    60. and here's the referenced player script
    61.  
    62. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;    //Allows us to use UI.
    5. using UnityEngine.SceneManagement;
    6. public enum Direction { LEFT, RIGHT };
    7. public class PlayerPlatformerController : PhysicsObject {
    8.  
    9.  
    10.  
    11.     public float maxSpeed = 3f;
    12.     public float jumpTakeOffSpeed = 5f;
    13.     public float flyingPower = 2.25f;
    14.  
    15.     public int slashPower = 3;
    16.     public int shootPower = 1;
    17.  
    18.     public Text HealthText;
    19.     public Text EnergyText;
    20.  
    21.     public bool isShooting = false;
    22.     public bool isSlashing = false;
    23.     public bool isFlying = false;
    24.  
    25.     public int energyPoints = 20;
    26.  
    27.     private SpriteRenderer spriteRenderer;
    28.     private Animator animator;
    29.     private Direction playerDirection = Direction.RIGHT;
    30.  
    31.     //private Transform _transform;
    32.     public GameObject Energy;
    33.  
    34.  
    35.     protected void Start()
    36.     {
    37.         spriteRenderer = GetComponent<SpriteRenderer> ();
    38.         animator = GetComponent<Animator> ();
    39.         //_transform = GetComponent(typeof(Transform)) as Transform;
    40.  
    41.       //  energy = GameManager.instance.energyPoints;
    42.        
    43.     }
    44.  
    45.     private void OnDisable()
    46.     {
    47.        // GameManager.instance.energyPoints = energy;
    48.     }
    49.  
    50.  
    51.     public Direction PlayerDirection
    52.     {
    53.  
    54.         get
    55.         {
    56.             return playerDirection;
    57.         }
    58.     }
    59.  
    60.     void Update()
    61.     {
    62.  
    63.         AttackSlash();
    64.  
    65.         ComputeVelocity();
    66.  
    67.         AttackEnergy();
    68.        
    69.     }
    70.  
    71.  
    72.  
    73.  
    74.     void AttackSlash()
    75.     {
    76.         if (Input.GetKeyDown(KeyCode.Z))
    77.         {
    78.  
    79.             animator.SetTrigger("PlayerSlash");
    80.  
    81.         }
    82.  
    83.  
    84.     }
    85.  
    86.  
    87.  
    88.     void AttackEnergy()
    89.     {
    90.      
    91.        
    92.            
    93.  
    94.         if (Input.GetKeyDown(KeyCode.X) && energyPoints > 0 )
    95.         {
    96.          
    97.  
    98.             var tEnergy = Instantiate(Energy, gameObject.transform.position, Energy.transform.rotation) as GameObject;
    99.             tEnergy.GetComponent<Energy>().energyDirection = PlayerDirection;
    100.             animator.SetTrigger("PlayerEnergy");
    101.  
    102.             energyPoints--;
    103.  
    104.             isShooting = true;
    105.  
    106.             if (!Input.GetKeyUp(KeyCode.X))
    107.             {
    108.                 isShooting = true;
    109.             }
    110.  
    111.         }
    112.  
    113.        
    114.  
    115.         if (Input.GetKeyUp(KeyCode.X))
    116.         {
    117.            
    118.             isShooting = false;
    119.  
    120.         }
    121.  
    122.         if (energyPoints < 0)
    123.         {
    124.             energyPoints = 0;
    125.         }
    126.  
    127.     }
    128.  
    129.  
    130.  
    131.  
    132.     protected override void ComputeVelocity()
    133.     {
    134.  
    135.      
    136.         Vector2 move = Vector2.zero;
    137.  
    138.         move.x = Input.GetAxis ("Horizontal");
    139.  
    140.     if (move.x > 0.01f)
    141.     {
    142.         playerDirection = Direction.RIGHT;
    143.  
    144.     }
    145.     else if (move.x < -0.01f)
    146.     {
    147.         playerDirection = Direction.LEFT;
    148.  
    149.     }
    150.  
    151.  
    152.  
    153.     if (Input.GetButtonDown ("Jump") && grounded) {
    154.             velocity.y = jumpTakeOffSpeed;
    155.             animator.SetTrigger("PlayerFlap");
    156.             isFlying = true;
    157.             animator.SetTrigger("PlayerFly");
    158.  
    159.  
    160.         } else if (Input.GetButtonDown ("Jump"))
    161.         {
    162.             animator.SetTrigger("PlayerFlap");
    163.             velocity.y = flyingPower;
    164.             isFlying = true;
    165.             animator.SetTrigger("PlayerFly");
    166.         }
    167.  
    168.         if (move.x > 0.01f)
    169.         {
    170.             animator.SetTrigger("PlayerMove");
    171.             if (spriteRenderer.flipX == true)
    172.             {
    173.                 spriteRenderer.flipX = false;
    174.             }
    175.         }
    176.         else if (move.x < -0.01f)
    177.         {
    178.             animator.SetTrigger("PlayerMove");
    179.             if (spriteRenderer.flipX == false)
    180.             {
    181.                 spriteRenderer.flipX = true;
    182.             }
    183.         }
    184.  
    185.         animator.SetBool ("grounded", grounded);
    186.         animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);
    187.        // animator.SetBool("flying", isFlying);
    188.  
    189.         targetVelocity = move * maxSpeed;
    190.     }
    191. }

    I'm at my wits end, especially since I'm only a novice coder. uni.png uniplay.PNG uniply.PNG
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Code (CSharp):
    1. energyPoints = Player.GetComponent<PlayerPlatformerController>().energyPoints;
    You're assigning the value of an int to another int once in Start, so the initial value of PlayerPlatformerController.energyPoints is saved in the local energyPoints, but never updated.

    Some options for you:

    -Move the above line from Start to the beginning of Update
    -Save Player.GetComponent<PlayerPlatformerController>() to some variable like platController, and check platController.energyPoints in your if statements
    -Use ref to get a reference to the variable instead of just the value

    Let us know if you get it working.
     
    Last edited: May 21, 2018
  3. Lagger625

    Lagger625

    Joined:
    Mar 7, 2016
    Posts:
    10
    Hyblademin pretty much covered it. I have some further thoughts for you so this doesn't happen in the future. Here,
    energyPoints
    is an
    int
    , which is a value type, but you used it like a class, or a reference type.
    Primitive types, like int, float, double, etc. are all value types, so when declaring variables of those types and assigning them, you're effectively copying the value from an area in memory to another. If one of them is later changed, the other doesn't get affected. This is what you did with energyPoints at Start(), you assigned it once but never update it.
    Classes are reference types, like your PlayerPlatformerController, or Unity's GameObject, or Transform, etc. If you declare a variable of a reference type and assign it, what gets copied is a reference to the same object in memory, so if that object changes, the variables that reference it see the changes in it's methods and properties. This is what you have to do, to save a reference to the PlayerPlatformerController at Start() and in Update read the energyPoints from it.

    In the Image script change
    public int energyPoints;
    to
    private PlayerPlatformerController ppc;
    , and then in Update change
    if (energyPoints <= 5)
    to
    if (ppc.energyPoints <= 5)
     
    DiamondEyes likes this.
  4. DiamondEyes

    DiamondEyes

    Joined:
    May 20, 2018
    Posts:
    3
    Thanks very much! I will implement this immediately, thanks for the help!
     
  5. Lagger625

    Lagger625

    Joined:
    Mar 7, 2016
    Posts:
    10
    No problem. And sorry, I forgot to mention something. In the Image script, Start() method, change this

    energyPoints = Player.GetComponent<PlayerPlatformerController>().energyPoints;


    to this

    ppc = Player.GetComponent<PlayerPlatformerController>();


    Did you figure it out on your own already? :p