Help with my code?

Hello,

Sorry, not sure where or how to post this correctly but here goes,

I have 2 different scripts on two different objects, a player controller and a powerup with its own script. The power up script needs to update a float value in the player controller (jumpEnergy) when the power up is collected.

Right now it works, but every time I drop a power up into the level I have to define the “Player” in the inspector by pointing to my Player entity in the hierarchy. This makes level design time consuming and will probably not work if they are spawned during gameplay.

I’m pretty sure that I can have the Player targeted in the script so I don’t have to find it each time I add the powerup to a map, Can anyone help me achieve this please?

And Thank you.

The two scripts are below:

playerCntroller.cs

public class playerController : MonoBehaviour {

    public float speed;

    public float jumpEnergy; //jump enery amount at any time
    public float jumpForce; //energy used per jump
//    public float distanceToGround;
    private Rigidbody rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void Update ()
    {
        PlayerMovement();
        if (Input.GetKeyDown (KeyCode.Space) && (jumpEnergy > 1f))
        {
            PlayerJump ();
        }
    }
     
    void PlayerMovement()
    {
        float hor = Input.GetAxis ("Horizontal");
        float ver = Input.GetAxis ("Vertical");
        Vector3 playerMovement = new Vector3(hor, 0f, ver) * speed * Time.deltaTime;
        transform.Translate(playerMovement, Space.Self);
    }

    void PlayerJump()
    {
        rb.AddForce (Vector3.up * jumpForce, ForceMode.Impulse);
        jumpEnergy = (jumpEnergy - jumpForce);
    }

}

And the powerup script
pickUpJumpEnergy.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pickUpJumpEnergy : MonoBehaviour

{
    public playerController player;
    public static float pickupsize = 50f;
    //public AudioSource collectSound;

    void Awake()
    {
           player = player.GetComponent <playerController>();
    }

    void ApplyPowerUp ()
    {
        print ("Item Picked Up");
        Destroy (gameObject);
        player.jumpEnergy = (player.jumpEnergy + pickupsize);
    }

    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.tag == "Player") 
        {
    //      collectSound.Play ();
        ApplyPowerUp();
        }
    }
}

You could search for the player in awake/start, create a singleton, use a scriptable object, or get the player object in the ontrigger enter method. Personally, I prefer using scriptable objects to communicate between objects but ontrigger enter method would probably be the best solution for your case.

Finding the player in awakeplayer = FindObjectOfType<playerController>();a singleton

public class PlayerController : MonoBehaviour
{
    public static PlayerController Instance;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
            return;
        }

        Instance = this;
    }

    public void DoSomethingOnPlayer() { }
}

public class Interactable : MonoBehaviour
{
    private void DoSomethingWithPlayer()
    {
        PlayerController.Instance?.DoSomethingOnPlayer();
    }
}

Checking for playing in on trigger method

    private void OnTriggerEnter(Collider other)
    {
        var player = other.gameObject.GetComponent<PlayerController>();

        if (player == null)
            return;

        player.jumpEnergy += pickupsize;
        Destroy(gameObject);
    }
1 Like

Thanks, that worked :slight_smile: