- Home /
 
SpeedBoost not working,My speedBoostTaken variable isn't working, why?
,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);
 
 
 
     }
 }
 
 
              Answer by highpockets · May 28, 2019 at 09:15 PM
I’m quite sure your problem is in the “Character” class. You are referencing the SpeedBoost script speed = speed + sBoost.Boost and this is after you set the bool to true, but it is also after you Destroy the SpeedBoost script. Unless your Character class sBoost variable is a different instance, I would think that is your issue 
Your answer
 
             Follow this Question
Related Questions
How can you make an object visible in Unity when you jump on it? 0 Answers
How to activate gameObjects based on a boolean from another scene? 1 Answer
Boolean Troubles(C#)- Can anyone help? 1 Answer
Distribute terrain in zones 3 Answers
Can someone help me with the jumping in this Player Controller script? 1 Answer