,So i’m making a small tribute game to the world’s hardest game and I’m trying to make a speedBoost item, I’ve made a bool that becomes true if the item is taken and destroys the speedBoost item, the item does detroy itself, however the speedBoostTaken variable doesn’t change.Why?
SpeedBoost script :
public class SpeedBoost : MonoBehaviour
{
public Character character;
public int Boost;
//Duration not used yet
public int Duration;
void OnTriggerEnter2D(Collider2D trigger)
{
if (trigger.gameObject.name == "character")
{
character.GetComponent<Character>().speedBoostTaken = true;
Debug.Log("Destroy Object");
Destroy(gameObject);
}
}
}
Character script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
public SpeedBoost sBoost;
public float speed; //Floating point variable to store the player's movement speed.
public bool speedBoostTaken = false;
public Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
private void Awake()
{
}
void Start()
{
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D>();
rb2d.name = "character";
sBoost = GetComponent<SpeedBoost>();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
if (speedBoostTaken == true)
{
Debug.Log("Speed Boost Taken");
speed = speed + sBoost.Boost;
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
//Use the two store floats to create a new Vector2 variable movement.
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
rb2d.AddForce(movement * speed);
}
}