Right guys struggling here! new to code... My first object (power up) is being destroyed but no power function is being generated, second object and there after works perfectly. PLEASE HELP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUps : MonoBehaviour
{
public bool doublePoints;
public float powerupLength;
private PowerupManager thePowerupManager;
// Start is called before the first frame update
void Start()
{
thePowerupManager = FindObjectOfType<PowerupManager>();
}
void Awake()
{
doublePoints = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
thePowerupManager.ActivatePowerup(doublePoints, powerupLength);
gameObject.SetActive(false);
}
}
}
-------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PowerupManager : MonoBehaviour
{
private bool doublePoints;
public Image dxp;
private bool powerupActive;
private float powerupLengthCounter;
private ScoreManager theScoreManager;
private PlatformGenerator thePlatformGenerator;
private GameManager theGameManager;
private float normalPointsPerSecond;
// Start is called before the first frame update
void Start()
{
theScoreManager = FindObjectOfType<ScoreManager>();
thePlatformGenerator = FindObjectOfType<PlatformGenerator>();
theGameManager = FindObjectOfType<GameManager>();
dxp.enabled = false;
}
// Update is called once per frame
void Update()
{
if(powerupActive)
{
powerupLengthCounter -= Time.deltaTime;
dxp.enabled = true;
if(theGameManager.powerupReset)
{
powerupLengthCounter = 0;
theGameManager.powerupReset = false;
}
if(doublePoints)
{
theScoreManager.pointsPerSecond = normalPointsPerSecond * 2;
theScoreManager.shouldDouble = true;
}
if(powerupLengthCounter <= 0)
{
theScoreManager.pointsPerSecond = normalPointsPerSecond;
theScoreManager.shouldDouble = false;
powerupActive = false;
}
}
if(!powerupActive)
{
dxp.enabled = false;
}
}
public void ActivatePowerup(bool points, float time)
{
doublePoints = points;
powerupLengthCounter = time;
if(!powerupActive)
{
normalPointsPerSecond = theScoreManager.pointsPerSecond;
}
powerupActive = true;
}
}
You sure have a lot of boolean variables. Do you really need doublePoints? Can you not just use dxp.enabled, or theScore$$anonymous$$anager.shouldDouble?
$$anonymous$$ore importantly when is ActivatePowerup being called and how?
Finally, your indentation is inconsistent try this:
public class Powerup$$anonymous$$anager : $$anonymous$$onoBehaviour
{
private bool doublePoints;
public Image dxp;
private bool powerupActive;
private float powerupLengthCounter;
private Score$$anonymous$$anager theScore$$anonymous$$anager;
private PlatformGenerator thePlatformGenerator;
private Game$$anonymous$$anager theGame$$anonymous$$anager;
private float normalPointsPerSecond;
// Start is called before the first frame update
void Start()
{
theScore$$anonymous$$anager = FindObjectOfType<Score$$anonymous$$anager>();
thePlatformGenerator = FindObjectOfType<PlatformGenerator>();
theGame$$anonymous$$anager = FindObjectOfType<Game$$anonymous$$anager>();
dxp.enabled = false;
}
// Update is called once per frame
void Update()
{
if(powerupActive)
{
powerupLengthCounter -= Time.deltaTime;
dxp.enabled = true;
if(theGame$$anonymous$$anager.powerupReset)
{
powerupLengthCounter = 0;
theGame$$anonymous$$anager.powerupReset = false;
}
if(doublePoints)
{
theScore$$anonymous$$anager.pointsPerSecond = normalPointsPerSecond * 2;
theScore$$anonymous$$anager.shouldDouble = true;
}
if(powerupLengthCounter <= 0)
{
theScore$$anonymous$$anager.pointsPerSecond = normalPointsPerSecond;
theScore$$anonymous$$anager.shouldDouble = false;
powerupActive = false;
}
}
if(!powerupActive) dxp.enabled = false;
}
public void ActivatePowerup(bool points, float time)
{
doublePoints = points;
powerupLengthCounter = time;
if(!powerupActive) normalPointsPerSecond = theScore$$anonymous$$anager.pointsPerSecond;
powerupActive = true;
}
}
Hello ShineBolt, Thankyou for your reply and i have now replaced my code with yours and works fine. The error was actually a public boolean that i had set to active when should of been false by mistake, took me a while to figure it out :)
Oh, your welcome, but just fyi the indentation doesn't effect how the program runs at all. In fact most spacing doesn't change anything, it's just for readability. $$anonymous$$ost people indent the code of any block that isn't on one line, and will look to the indentation to find whether or not a line of code is part of a block, not the brackets.
Your answer
Follow this Question
Related Questions
Detect whether a Rigidbody collided with a Trigger 2 Answers
Cannot delay player respawn. 2 Answers
Teleport 2D Character on TriggerEnter 2 Answers
CS1216 error 0 Answers