Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with power pick up...

Discussion in 'Scripting' started by AEBlob, Apr 2, 2016.

  1. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    So I have a Player that has a power bar that enables it to use certain abilities. I have managed to get the power bar to go down when the ability keys are used, and the power bar and abilities stop at zero/empty. However, I'm having trouble getting the pick ups to work, to replenish the power bar.

    Here are the relevant codes...

    In the Player script...

    Code (CSharp):
    1. [SerializeField]
    2.     private Stat energy;
    3.     // called by Unity the moment the game is loaded
    4.     private void Awake()
    5.     {
    6.         myAnimator = GetComponent<Animator>();
    7.         myRigidbody = GetComponent<Rigidbody2D>();
    8.         // using an internal gravity variable, so the RigidBody should not apply its own
    9.         myRigidbody.gravityScale = 0;
    10.         energy.Initialize();
    11.     }
    12.     // called by Unity just before the game starts
    13.     private void Start()
    14.     {
    15.         facingRight = true;
    16.         SetState(startingState);
    17.         player = FindObjectOfType<Player>();
    18.     }
    19.     // called by Unity every single frame once the game has started
    20.     private void Update()
    21.     {
    22.         HandleInput();
    23.         Flip();
    24.         if (Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             energy.CurrentVal -= 10;
    27.         }
    28.         else if (Input.GetKeyDown(KeyCode.RightShift))
    29.         {
    30.             energy.CurrentVal -= 10;
    31.         }
    32.     }
    Stat script...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. [Serializable]
    5. public class Stat
    6. {
    7.     [SerializeField]
    8.     private BarScript bar;
    9.     [SerializeField]
    10.     public float maxVal;
    11.     [SerializeField]
    12.     public float currentVal;
    13.     public float CurrentVal
    14.     {
    15.         get
    16.         {
    17.             return currentVal;
    18.         }
    19.         set
    20.         {
    21.             this.currentVal = Mathf.Clamp(value,0,MaxVal);
    22.             bar.Value = currentVal;
    23.         }
    24.     }
    25.     public float MaxVal
    26.     {
    27.         get
    28.         {
    29.             return maxVal;
    30.         }
    31.         set
    32.         {
    33.             this.maxVal = value;
    34.             bar.MaxValue = maxVal;
    35.         }
    36.     }
    37.     public void Initialize()
    38.     {
    39.         this.MaxVal = maxVal;
    40.         this.CurrentVal = currentVal;
    41.     }
    42. }
    ... and code for the Pick Up...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PowerPickup : MonoBehaviour {
    4.     public float powerAmount;
    5.     private Stat energy;
    6.     private Player player;
    7.     // Use this for initialization
    8.     void Start () {
    9.         player = GetComponent<Player>();
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16.     void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if (other.tag == "Player" && energy.currentVal < energy.maxVal)
    19.         {
    20.             energy.currentVal += 10;
    21.             Destroy(gameObject);
    22.         }
    23.     }
    24. }
    I tried several other things, this is just the state of things as they stand currently. What am I missing? I'm still trying to get the hang of coding, I'd never done any before until a few months ago, so any help will be greatly appreciated. :)
     
  2. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    I'll just add, this is a 2D project and the Pick Up object has a BoxCollider2D, that is Trigger.
     
  3. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Sorry, bump :)
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    In the OnTriggerEnter2D() do
    Debug.Log(other.tag);
    and then check console when it's supposed to hit.

    I'm assuming that player has a subobject with collider that hits this powerup. And it may be untagged.

    Why does powerup have Player script though?
     
    AEBlob likes this.
  5. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Hey Zaflis, thanks for your reply. What do you mean by a subobject? Child object? My Player has 3 PolygonColliders, as he changes between 3 different states.

    The power up doesn't have a Player script, that's just on the Player, it just contains the code regarding the reduction of the Power every time he uses his power, if that makes sense.
     
  6. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    OK, the error I got was...

     
  7. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Yeah meant child object.

    In there you do have Player script, acquired from the PowerPickup gameobject itself. If you want it to link to player object, make it "public", and assign value in inspector, dragging player object to it.
    Code (CSharp):
    1. public class PowerPickup : MonoBehaviour {
    2. ...
    3.     private Stat energy;
    4.     private Player player;
    5.  
    6.     void Start () {
    7.         player = GetComponent<Player>();
    8.     }
    The error you get may be from same place, you have Stat energy but you haven't made a new instance for it.
     
  8. AEBlob

    AEBlob

    Joined:
    Nov 20, 2015
    Posts:
    53
    Yeah I wasn't sure what I was doing to be honest so I don't think that reference to the Player is necessary, I'm really not sure to be honest.