Scenes are not loading - c#

Something really odd happens when I'm trying to load a scene.
The code that generates an error (I guess):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class playSc : MonoBehaviour {
public string levelToLoad;
public Animator menumusic;
public GameObject background;
public GameObject text;
public GameObject progressSlider;
public Text progressText;
public Slider progressBar;
private int loadProgress = 0;
public void close()
{
Application.Quit();
}
public void play()
{
menumusic.SetBool("FadeOUT", true);
GameObject.Find("Clock").SetActive(false);
progressText.text = "Think about the Universe to continue.\nThinking progress:";
StartCoroutine(DisplayLoadingScreen("Game"));
}
public void credits()
{
menumusic.SetBool("FadeOUT", true);
GameObject.Find("Clock").SetActive(false);
progressText.text = "Think about your behaviour.\nThinking progress:";
StartCoroutine(DisplayLoadingScreen("Credits"));
}
IEnumerator DisplayLoadingScreen(string level)
{
background.SetActive(true);
text.SetActive(true);
progressSlider.SetActive(true);
AsyncOperation async = SceneManager.LoadSceneAsync(level);
while (!async.isDone)
{
loadProgress = (int)(async.progress * 100);
progressBar.value = loadProgress;
yield return null;
}
}
}
I tried to use Application.LoadLevel() and it still is not working. It was working with this code but at one moment it stopped.

Related

UNity3d Adding a value via button and getting random results

i found my next tier 1 problem and hoping for some help here in the so called internet.
first of all here are my atm not to hard to understanding codes.
please tell me if im doing something wrong here on stackoverflow im an absolute newbie and dont know how to explain my problem in this case without post a bunch of code :(
i know there are much ways to improve mystuff but im in the learning process and for now its maybe enough to tell my why the behavior of my code didnt match my expectations.
This is my PlayerClass:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Values : MonoBehaviour
{
public float MaxNrg;
public float Nrg;
public float NrgDrain;
public float NrgDrainCoef;
public float NrgInPercent;
// Start is called before the first frame update
public void Start()
{
MaxNrg = 7950.0f;
Nrg = 7950.0f;
NrgDrain = 0.09201389f;
NrgDrainCoef = 1.0f;
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
NrgInPercent = (GETnrg() / GETmaxnrg()) * 100;
}
public void SETmaxnrg(float tmpfloat)
{
MaxNrg = tmpfloat;
}
public void SETnrg(float tmpfloat)
{
Nrg = tmpfloat;
}
public void SETnrgdrain(float tmpfloat)
{
NrgDrain = tmpfloat;
}
public void SETnrgdraincoef(float tmpfloat)
{
NrgDrainCoef = tmpfloat;
}
public void ADDmaxnrg(float tmpfloat)
{
MaxNrg += tmpfloat;
}
public void ADDnrg(float tmpfloat)
{
Nrg += tmpfloat;
}
public void ADDnrgdrain(float tmpfloat)
{
NrgDrain += tmpfloat;
}
public void ADDnrgdraincoef(float tmpfloat)
{
NrgDrainCoef += tmpfloat;
}
public float GETmaxnrg()
{
return MaxNrg;
}
public float GETnrg()
{
return Nrg;
}
public float GETnrgdrain()
{
return NrgDrain;
}
public float GETnrgdraincoef()
{
return NrgDrainCoef;
}
public float GETnrginpercent()
{
return NrgInPercent;
}
}
End Here are the "Drain_mechanics":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drain_Mechanics : MonoBehaviour
{
public bool drainstate = false;
public double NrgLeftInSeconds;
public GameObject Scriptcontainer_Player;
// Start is called before the first frame update
public void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
if(drainstate == true)
{
Scriptcontainer_Player.GetComponent<Player_Values>().Nrg -= Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain() * Time.deltaTime * Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef();
}
else if(drainstate == false)
{
}
NrgLeftInSeconds = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrg() / (Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain() * Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef());
}
public void SETdrainstate(bool tmpdrainstate)
{
drainstate = tmpdrainstate;
}
public double GETnrgleftinseconds()
{
return NrgLeftInSeconds;
}
}
Class for initializing the input fields in the game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Initialize_InputFields : MonoBehaviour
{
public GameObject Input_Energy;
public GameObject Input_Drainrate;
public GameObject Input_DrainCoef;
public float InputNrg;
public float InputNrgDrainrate;
public float InputNrgDrainCoef;
// Start is called before the first frame update
public void Start()
{
Input_Energy = GameObject.Find("Input_Energy");
Input_Drainrate = GameObject.Find("Input_Drainrate");
Input_DrainCoef = GameObject.Find("Input_DrainCoef");
}
// Update is called once per frame
void FixedUpdate()
{
InputNrg = float.Parse(Input_Energy.GetComponent<InputField>().text);
InputNrgDrainrate = float.Parse(Input_Drainrate.GetComponent<InputField>().text);
InputNrgDrainCoef = float.Parse(Input_DrainCoef.GetComponent<InputField>().text);
}
public float GETinputnrg()
{
return InputNrg;
}
public float GETinputnrgdrainrate()
{
return InputNrgDrainrate;
}
public float GETinputnrgdraincoef()
{
return InputNrgDrainCoef;
}
}
a class to manipulate the textfields while the game is running
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Display_LabelsCalculated : MonoBehaviour
{
public GameObject Scriptcontainer_Player;
public GameObject Scriptcontainer_GameMechanics;
public GameObject Display_Energy;
public GameObject Display_EnergyDrain;
public GameObject Display_EnergyDrainCoef;
public GameObject Display_EnergyTimeleft;
public GameObject Display_EnergyPercentleft;
public Text Display_Energy_Text;
public Text Display_EnergyDrain_Text;
public Text Display_EnergyDrainCoef_Text;
public Text Display_EnergyTimeleft_Text;
public Text Display_EnergyPercentleft_Text;
// Start is called before the first frame update
void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
Scriptcontainer_GameMechanics = GameObject.Find("Scriptcontainer_GameMechanics");
Display_Energy = GameObject.Find("Display_Energy");
Display_EnergyDrain = GameObject.Find("Display_EnergyDrain");
Display_EnergyDrainCoef = GameObject.Find("Display_EnergyDrainCoef");
Display_EnergyTimeleft = GameObject.Find("Display_EnergyTimeleft");
Display_EnergyPercentleft = GameObject.Find("Display_EnergyPercentleft");
Display_Energy_Text = Display_Energy.GetComponent<Text>();
Display_EnergyDrain_Text = Display_EnergyDrain.GetComponent<Text>();
Display_EnergyDrainCoef_Text = Display_EnergyDrainCoef.GetComponent<Text>();
Display_EnergyTimeleft_Text = Display_EnergyTimeleft.GetComponent<Text>();
Display_EnergyPercentleft_Text = Display_EnergyPercentleft.GetComponent<Text>();
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
int hours = (int)(Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() / 60) / 60;
int minutes = (int)(Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() / 60) % 60;
int seconds = (int)Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() % 60;
Display_EnergyTimeleft_Text.text = hours.ToString() + ":" + ((minutes < 10) ? ("0") : ("")) + minutes.ToString() + ":" + ((seconds < 10) ? ("0") : ("")) + seconds.ToString();
Display_Energy_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrg().ToString();
Display_EnergyDrain_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain().ToString();
Display_EnergyDrainCoef_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef().ToString();
Display_EnergyPercentleft_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrginpercent().ToString();
}
}
followed by some button mechanics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Button_Mechanics : MonoBehaviour
{
public GameObject Scriptcontainer_Player;
public GameObject Scriptcontainer_InputFields;
public GameObject Scriptcontainer_GameMechanics;
public Button Button_SetEnergy;
public Button Button_SetDrainCoef;
public Button Button_SetDrainrate;
public Button Button_AddEnergy;
public Button Button_AddDrainCoef;
public Button Button_AddDrainrate;
public Button Button_ActivateMechanics;
public Button Button_DeactivateMechanics;
public Button Button_ResetValues;
// Start is called before the first frame update
public void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
Scriptcontainer_InputFields = GameObject.Find("Scriptcontainer_InputFields");
Scriptcontainer_GameMechanics = GameObject.Find("Scriptcontainer_GameMechanics");
Button_SetEnergy = GameObject.Find("Button_SetEnergy").GetComponent<Button>();
Button_SetDrainCoef = GameObject.Find("Button_SetDrainCoef").GetComponent<Button>();
Button_SetDrainrate = GameObject.Find("Button_SetDrainrate").GetComponent<Button>();
Button_AddEnergy = GameObject.Find("Button_AddEnergy").GetComponent<Button>();
Button_AddDrainCoef = GameObject.Find("Button_AddDrainCoef").GetComponent<Button>();
Button_AddDrainrate = GameObject.Find("Button_AddDrainrate").GetComponent<Button>();
Button_ActivateMechanics = GameObject.Find("Button_ActivateMechanics").GetComponent<Button>();
Button_DeactivateMechanics = GameObject.Find("Button_DeactivateMechanics").GetComponent<Button>();
Button_ResetValues = GameObject.Find("Button_ResetValues").GetComponent<Button>();
}
public void Update()
{
}
// Update is called once per frame
public void FixedUpdate()
{
Button_SetEnergy.onClick.AddListener(SETnrgbtnclk);
Button_SetDrainCoef.onClick.AddListener(SETnrgdraincoefbtnclk);
Button_SetDrainrate.onClick.AddListener(SETnrgdrainbtnclk);
Button_AddEnergy.onClick.AddListener(ADDnrgbtnclk);
Button_AddDrainCoef.onClick.AddListener(ADDnrgdraincoefbtnclk);
Button_AddDrainrate.onClick.AddListener(ADDnrgdrainbtnclk);
Button_ActivateMechanics.onClick.AddListener(SETactivatemechanicsbtnclk);
Button_DeactivateMechanics.onClick.AddListener(SETdeactivatemechanicsbtnclk);
Button_ResetValues.onClick.AddListener(SETresetbtnclk);
}
public void SETnrgbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrg(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrg());
}
public void SETnrgdrainbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdrain(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdrainrate());
}
public void SETnrgdraincoefbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdraincoef(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdraincoef());
}
public void ADDnrgbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrg(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrg());
}
public void ADDnrgdrainbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrgdrain(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdrainrate());
}
public void ADDnrgdraincoefbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrgdraincoef(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdraincoef());
}
public void SETactivatemechanicsbtnclk()
{
Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().SETdrainstate(true);
}
public void SETdeactivatemechanicsbtnclk()
{
Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().SETdrainstate(false);
}
public void SETresetbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrg(7950);
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdrain(0.09201389f);
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdraincoef(1.0f);
}
}
at this point i apollogize one more time the amount of text im posting here but otherwise i cant even explain whats going on. and im not able to pick the exact codesnippets which are needed here. it will be better in future.
basically the main mechnisms work fine and im absolutely proud of what i did here...
my problems are the following.
Major problem 1. when i enter a value in the input fields and perform the button SET actions, i HAVE to do it in a specific order. NRG->DRAIN->COEF
otherwise the calculated textboxes are doing random stuff.
Major problem 2. when i try to perform the button add actions, the game also do random stuff. even when i set all variable to fixed values.
minor problem 3. in the Initialize_InputFields.cs im using the float.parse functionality. and my game constantly create warnings about a formatexception. is there a smarter way to solve this.
minor problem 4. last but not leat i want to work with realtime values. is there a way in unity to accerlate the globale timescale for the whole game?

Event Action Doesn't work in state Pattern

I'm using state pattern for my unity game. I have three states; HappyState, SurprisedState and SadState. HappyState is default state. I want the character to jump and enter surprised state by clicking left-mouse. Entering surprised state, a few jobs need to be done which I defined as a void to be subscribed to an event, But it doesn't work!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class CapsuleScript : MonoBehaviour
{
public event Action SliderAction;
public Slider slider;
public Text stateText;
public GameObject hands;
public Rigidbody rb;
public CapsuleBasicStates currentstate;
public readonly HappyState happyState = new HappyState();
public readonly SadState sadState = new SadState();
public readonly SurprisedState surprisedState = new SurprisedState();
public SpriteRenderer renderer;
public Sprite happysprite, sadsprite, surprisedsprite;
// Start is called before the first frame update
void Start()
{
slider.value = 0;
renderer = GetComponentInChildren<SpriteRenderer>();
rb = GetComponent<Rigidbody>();
TransitionToState(happyState);
}
public void OnCollisionEnter(Collision other) {
currentstate.OnCollisionEnter(this);
}
// Update is called once per frame
void Update()
{
currentstate.Update(this);
}
public void SetSprite(Sprite sprite)
{
renderer.sprite = sprite;
}
public void TransitionToState(CapsuleBasicStates state)
{
currentstate = state;
currentstate.EnterState(this);
}
public IEnumerator SliderHandler()
{
yield return new WaitForSeconds(1f);
slider.value +=1;
StartCoroutine(SliderHandler());
}
public void IEHandler()
{
StartCoroutine(SliderHandler());
}
}
Here is SurprisedState script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SurprisedState : CapsuleBasicStates
{
public override void EnterState(CapsuleScript player)
{
player.SetSprite(player.surprisedsprite);
player.stateText.text = player.surprisedState.ToString();
player.slider.gameObject.SetActive(true);
player.SliderAction += player.IEHandler;
}
public override void OnCollisionEnter(CapsuleScript player)
{
//player.TransitionToState(player.happyState);
}
public override void Update(CapsuleScript player)
{
if(player.slider.value ==20)
{
player.SliderAction -= player.IEHandler;
player.TransitionToState(player.happyState);
}
}
}
HappyState script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HappyState : CapsuleBasicStates
{
public override void EnterState(CapsuleScript player)
{
player.SetSprite(player.happysprite);
player.stateText.text = player.happyState.ToString();
player.hands.SetActive(false);
}
public override void OnCollisionEnter(CapsuleScript player)
{
}
public override void Update(CapsuleScript player)
{
if(Input.GetButton("Fire1"))
{
player.rb.AddForce(Vector3.up * 150f);
player.TransitionToState(player.surprisedState);
}
if(Input.GetButton("Fire2"))
{
player.rb.AddForce(Vector3.up *250f);
player.TransitionToState(player.sadState);
}
}
}
Few points before I post my solution:
Try to post all scripts related to the question you are asking.
Comment your code or try to explain what each part of your script is doing.
BaseSate.cs
[System.Serializable]
public abstract class BaseState
{
public enum PlayerStates { HappyState = 0, SurprisedState = 1 }
public abstract PlayerStates PlayerState { get; }
public abstract void EnterState(PlayerScript playerScript);
public abstract void UpdateState();
public abstract void ExitState();
}
HappySate.cs
using UnityEngine;
[System.Serializable]
public class HappyState : BaseState
{
public override PlayerStates PlayerState => PlayerStates.HappyState;
private PlayerScript _playerScript;
public override void EnterState(PlayerScript playerScript)
{
_playerScript = playerScript;
Debug.Log($"Entered {PlayerState}");
}
public override void UpdateState()
{
if (Input.GetButtonDown("Fire1"))
{
_playerScript.ChangeState(new SurprisedState());
}
}
public override void ExitState()
{
Debug.Log($"Exited {PlayerState}");
}
}
SurprisedState.cs
using UnityEngine;
[System.Serializable]
public class SurprisedState : BaseState
{
public override PlayerStates PlayerState => PlayerStates.SurprisedState;
private PlayerScript _playerScript;
public override void EnterState(PlayerScript playerScript)
{
_playerScript = playerScript;
// subscribe to sliderAction on enter
playerScript.playerSliderAction += OnSliderChange;
Debug.Log($"Entered {PlayerState}");
}
private void OnSliderChange(float sliderValue)
{
// use Mathf.Approximately instead of == when comparing
// floating numbers.
if (Mathf.Approximately(sliderValue, 20f))
{
_playerScript.ChangeState(new HappyState());
}
}
public override void UpdateState() { }
public override void ExitState()
{
// unSubscribe to sliderAction on exit
_playerScript.playerSliderAction -= OnSliderChange;
Debug.Log($"Exited {PlayerState}");
}
}
PlayerScript.cs
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class PlayerScript : MonoBehaviour
{
[SerializeField]
private SpriteRenderer playerRenderer;
[SerializeField]
private Sprite[] playerStateSprites;
[SerializeField]
private Slider playerSlider;
public UnityAction<float> playerSliderAction;
private BaseState _currentState;
private void Awake()
{
Initialize();
}
private void Update()
{
_currentState.UpdateState();
}
private void Initialize()
{
ChangeState(new HappyState());
// use slider onValueChanged instead of checking slider
// value every frame.
// invoke unity action when the value has been changed.
playerSlider.onValueChanged.AddListener(sliderValue =>
{
playerSliderAction?.Invoke(sliderValue);
});
}
public void ChangeState(BaseState newState)
{
// change state only when it is different
// from previous state.
if (_currentState == newState)
{
return;
}
_currentState?.ExitState();
_currentState = newState;
_currentState.EnterState(this);
playerRenderer.sprite = playerStateSprites[(int)newState.PlayerState];
Debug.Log($"Current State : {_currentState.PlayerState}");
}
}
PlayerScript inspector view
Execution Overview gif:
https://gfycat.com/grimflowerycollardlizard

OnTriggerExit2D getting called unnecessarily

I hope you all are doing well. I have been following a Unity tutorial for a rhythm game and I have found this bug that I could not get past. Essentially, my OnTriggerExit2D is getting called too early. I'll include a picture in the conclusion of this post. I have tried logging the game object and it seems that all of my button objects suffer the same fate. I have included a link of the tutorial that I have been following in the conclusion. Any help towards figuring this out would be helpful.
Tutorial Link: https://www.youtube.com/watch?v=PMfhS-kEvc0&ab_channel=gamesplusjames
What my game looks like, the missed shows up when I've hit it.
Debug Output
GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public AudioSource theMusic;
public bool startPlaying;
public BeatScroller theBS;
public static GameManager instance;
public int currentScore;
public int scorePerNote = 100;
public int scorePerGoodNote = 125;
public int scorePerPerfectNote = 150;
public int currentMultiplier;
public int multiplierTracker;
public int [] multiplierTresholds;
public Text scoreText;
public Text multiText;
// Start is called before the first frame update
void Start()
{
instance = this;
scoreText.text = "Score: 0";
multiText.text = "Multiplier: x1";
currentMultiplier = 1;
}
// Update is called once per frame
void Update()
{
if(!startPlaying){
if(Input.anyKeyDown){
startPlaying = true;
theBS.hasStarted = true;
theMusic.Play();
}
}
}
public void NoteHit(){
Debug.Log("Note Hit On Time");
if(currentMultiplier-1 < multiplierTresholds.Length){
multiplierTracker++;
if(multiplierTresholds[currentMultiplier-1] <= multiplierTracker){
multiplierTracker = 0;
currentMultiplier++;
}
}
multiText.text = "Multiplier: x"+currentMultiplier;
//currentScore += scorePerNote * currentMultiplier;
scoreText.text = "Score: "+currentScore;
}
public void NormalHit(){
currentScore += scorePerNote * currentMultiplier;
NoteHit();
}
public void GoodHit(){
currentScore += scorePerGoodNote * currentMultiplier;
NoteHit();
}
public void PerfectHit(){
currentScore += scorePerPerfectNote * currentMultiplier;
NoteHit();
}
public void NoteMissed(){
Debug.Log("MISSED!");
multiText.text = "Multiplier: x1";
}
}
BeatScroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeatScroller : MonoBehaviour
{
public float beatTempo;
public bool hasStarted;
// Start is called before the first frame update
void Start()
{
beatTempo = beatTempo / 60f;
}
// Update is called once per frame
void Update()
{
if(!hasStarted){
}else{
transform.position -= new Vector3(0f, beatTempo*Time.deltaTime, 0f);
}
}
}
ButtonController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonController : MonoBehaviour
{
// Start is called before the first frame update
private SpriteRenderer theSR;
public Sprite defaultImage;
public Sprite pressedImage;
public KeyCode keyToPress;
void Start()
{
theSR = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(keyToPress))
{
theSR.sprite = pressedImage;
}
if(Input.GetKeyUp(keyToPress))
{
theSR.sprite = defaultImage;
}
}
}
noteObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class noteObject : MonoBehaviour
{
public bool canBePressed;
public KeyCode KeyToPress;
public GameObject hitEffect, goodEffect, perfectEffect, missedEffect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyToPress))
{
if(canBePressed)
{
gameObject.SetActive(false);
if(Mathf.Abs(transform.position.y) > 0.25){
GameManager.instance.NormalHit();
Debug.Log("Normal Hit!");
Instantiate(hitEffect,transform.position, hitEffect.transform.rotation);
}else if(Mathf.Abs(transform.position.y) > 0.05f){
GameManager.instance.GoodHit();
Debug.Log("Good Hit!!");
Instantiate(goodEffect,transform.position, goodEffect.transform.rotation);
}else{
GameManager.instance.PerfectHit();
Debug.Log("PERFECT HIT!!!");
Instantiate(perfectEffect,transform.position, perfectEffect.transform.rotation);
}
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Activator")
{
canBePressed = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Activator")
{
Debug.Log("Exited collider on game object: "+ other.gameObject.name);
canBePressed = false;
GameManager.instance.NoteMissed();
Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);
}
}
}
EffectObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EffectObject : MonoBehaviour
{
public float lifeTime = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Destroy(gameObject, lifeTime);
}
}
Hmm ... You say that OnTriggerExit2D is called to early? I assume it's called when the elements are still inside one another? If that's the case I guess your bounding boxes don't have the right size, or the right shape. I see arrows, do they have a rectangular bounding box or a polygon one that follows their shape? Are all your bounding boxes the right size?
I figured out what was wrong thanks to AdrAs's comment.
Turns out I had to check the y position of my arrow and collider were well enough below the height of my button box collider. In addition to that, I reshaped my colliders. I found that this code did the trick for me. Thank You All For the Nudges in the right direction.
noteObject -> new OnTriggerExit2D
private void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Activator" && transform.position.y < -0.32)
{
Debug.Log("Exited collider on game object: "+ other.gameObject.name);
canBePressed = false;
GameManager.instance.NoteMissed();
Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);
}
}

Unity Quiz Game Score in C#

I am making a quiz game in the unity from the unity live session quiz game tutorials everything is working fine except somehow the score isn't working when i Click the button it should add 10 score to the Score. Here are the tutorials : https://unity3d.com/learn/tutorials/topics/scripting/intro-and-setup?playlist=17117 and the code for my Game Controller :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
public Text questionDisplayText;
public Text scoreDisplayText;
public Text timeRemainingDisplayText;
public SimpleObjectPool answerButtonObjectPool;
public Transform answerButtonParent;
public GameObject questionDisplay;
public GameObject roundEndDisplay;
private DataController dataController;
private RoundData currentRoundData;
private QuestionData[] questionPool;
private bool isRoundActive;
private float timeRemaining;
private int questionIndex;
private int playerScore;
private List<GameObject> answerButtonGameObjects = new List<GameObject>();
// Use this for initialization
void Start ()
{
dataController = FindObjectOfType<DataController> ();
currentRoundData = dataController.GetCurrentRoundData ();
questionPool = currentRoundData.questions;
timeRemaining = currentRoundData.timeLimitInSeconds;
UpdateTimeRemainingDisplay();
playerScore = 0;
questionIndex = 0;
ShowQuestion ();
isRoundActive = true;
}
private void ShowQuestion()
{
RemoveAnswerButtons ();
QuestionData questionData = questionPool [questionIndex];
questionDisplayText.text = questionData.questionText;
for (int i = 0; i < questionData.answers.Length; i++)
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.Setup(questionData.answers[i]);
}
}
private void RemoveAnswerButtons()
{
while (answerButtonGameObjects.Count > 0)
{
answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);
}
}
public void AnswerButtonClicked(bool isCorrect)
{
if (isCorrect)
{
playerScore += currentRoundData.pointsAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScore.ToString();
}
if (questionPool.Length > questionIndex + 1) {
questionIndex++;
ShowQuestion ();
} else
{
EndRound();
}
}
public void EndRound()
{
isRoundActive = false;
questionDisplay.SetActive (false);
roundEndDisplay.SetActive (true);
}
public void ReturnToMenu()
{
SceneManager.LoadScene ("MenuScreen");
}
private void UpdateTimeRemainingDisplay()
{
timeRemainingDisplayText.text = "Time: " + Mathf.Round (timeRemaining).ToString ();
}
// Update is called once per frame
void Update ()
{
if (isRoundActive)
{
timeRemaining -= Time.deltaTime;
UpdateTimeRemainingDisplay();
if (timeRemaining <= 0f)
{
EndRound();
}
}
}
}
and my answer Button Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AnswerButton : MonoBehaviour {
public Text answerText;
private AnswerData answerData;
private GameController GameController;
// Use this for initialization
void Start ()
{
GameController = FindObjectOfType<GameController> ();
}
public void Setup(AnswerData data)
{
answerData = data;
answerText.text = answerData.answerText;
}
public void HandleClick()
{
GameController.AnswerButtonClicked (answerData.isCorrect);
}
}
and Answer Data :
using UnityEngine;
using System.Collections;
[System.Serializable]
public class AnswerData
{
public string answerText;
public bool isCorrect;
}
If everything is working fine (the whole code gets executed correctly, which I presume at this point), you probably did not set the data correctly. In your Game Controller, you have the line
playerScore += currentRoundData.pointsAddedForCorrectAnswer;
in your AnswerButtonClicked method which should add an amount you defined to the score if the answer is correct. Since I presume that your whole code is running fine (I can't see your in-engine setup, only the code here, which looks like the one in the tutorial), this is probably the first location where to look at the error. This value is probably set in the Unity Inspector or via another script, so you may want to go check in other files or the Editor.
The next thing to check is, if the buttons are correctly linked via their event handler. This can be checked by looking at the inspector. In the tutorial series this step is done in part Click to answer at the end of the video.

Changing Int from another script

I want to change the playerCurScore integer (in ScoreManager script) from HarmEnemiesScript.
(Script attached to Object in Scene)
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public int playerScore;
public int playerCurScore;
public void Start()
{
playerScore = 0;
playerCurScore = 0;
}
public void Update()
{
playerCurScore = playerScore;
}
}
(Script attached on Enemy)
using UnityEngine;
using System.Collections;
public class HarmEnemies : MonoBehaviour
{
public float enemyHealth;
public float enemyCurHealth;
public float playerDamage;
public void Start()
{
enemyCurHealth = enemyHealth;
}
public void OnTriggerEnter(Collider theCollision)
{
if(theCollision.tag == "Fireball")
{
enemyCurHealth = enemyCurHealth - playerDamage;
Destroy (theCollision);
}
if(enemyCurHealth <= 0)
{
Destroy (this.gameObject);
}
}
}
So how can I change the playerCurScore Int from HarmEnemies. I know that I have to use GetComponent and I can use playerCurScore++;
You are making several mistakes. The main one is that the ScoreManager script should not be attached to the EnemyGameObject.
Make a separate GameObject and name it ScoreManager. Then find it in the scene and update the score.
ScoreManager s = GameObject.Find("ScoreManager");
s.SendMessage("incrementScore", amount);
In the ScoreManager class you need this:
private static int score;
public void incrementScore(int amount)
{
score += amount;
}
If you are clever you make the whole ScoreManager class static and then it wouldn't even have to be a MonoBehaviour.
You would then call it like this:
ScoreManager.incrementScore(amount);
Information about static classes.
I could fix it now, thank you for the static hint. my code looks like this now:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static float playerScore;
public Text scoreCount;
public void Update()
{
ScoreController ();
}
public void ScoreController()
{
scoreCount.text = "Score: " + playerScore;
Debug.Log ("Score: " + playerScore);
}
}
using UnityEngine;
using System.Collections;
public class HarmEnemies : MonoBehaviour
{
public float enemyHealth;
public float enemyCurHealth;
public float playerDamage;
public void Start()
{
enemyCurHealth = enemyHealth;
}
public void OnTriggerEnter(Collider theCollision)
{
if(theCollision.tag == "Fireball")
{
enemyCurHealth = enemyCurHealth - playerDamage;
Destroy (theCollision);
}
if(enemyCurHealth <= 0)
{
ScoreManager.playerScore = (ScoreManager.playerScore + 1);
Destroy (this.gameObject);
}
}
}

Categories