- Home /
Activating powerup on button click
Hi,
I'm trying to get my powerup (increased movement speed) to work when it has been picked up & only a button has been pressed, but I can't seem to get it working.
If I pick up my powerup(setting the bool to true), and I click the button, I get the error that it's not been set to an instance of an object. I know this is probably fixed easily most of the time.
I am referencing the script, so I don't really get why it's not recognizing it..
Please keep in mind though that I'm just a (Unity/programming) beginner, and this is the first project I've gotten so far with for now.
See the last part of the scripts). The bool's been set to public obviously, otherwise I'm not able to reference it anyway.
I deleted all unnecessary parts for readability.
//The following is my PLAYER script.
`[Header("PowerUpSpeed")]
[SerializeField]
private float multiplier = 1.5f;
[SerializeField]
private float PowerupDuration = 5f;
public bool SpeedPowerUpInInventory;
public void OnTriggerEnter(Collider other)
{
//Powerups
if (other.CompareTag("PowerUpSpeed"))
{
StartCoroutine(MoveSpeedPowerUp());
SpeedPowerUpInInventory = true; //Picked up powerup, therefore setting bool to true.
}
}
//Powerup Coroutines
public IEnumerator MoveSpeedPowerUp()
{
moveSpeed *= multiplier;
yield return new WaitForSeconds(PowerupDuration);
moveSpeed /= multiplier;
SpeedPowerUpInInventory = false; //Used it, meaning it is no longer in my inventory therefore bool = false.
yield return null;
}
//This is my Canvas' script. I dragged the canvas into the button into the OnClick() function. UseSpeedPowerup() is the function used by the button. public class GameUI : MonoBehaviour { PlayerController playerScript;
void Start()
{
playerScript = GetComponent<PlayerController>();
}
public void UseSpeedPowerup()
{
if (playerScript.SpeedPowerUpInInventory) //This is where I get the "Not set to instance of an object" error.
{
StartCoroutine(playerScript.MoveSpeedPowerUp());
}
}
}
This last script is the one sitting on the powerup itself, even though it doesn't contain much, I still thought it should be added to the thread.
using System.Collections;
using UnityEngine;
public class PowerUp : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
PickUp();
}
}
void PickUp()
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Infinite repetition, need help 1 Answer
First Person Controller Script not working 3 Answers
Can anyone help with me with my attack script and enemy script? 1 Answer
Quick code help! (c#) 2 Answers
Take object from player 0 Answers