- Home /
Question by
mihailomaksimovic12 · Jun 21, 2020 at 09:34 AM ·
invoke
I am using Invoke in one function but it don't work when function is activated near the end of the scene because time from Invoke didn't pass. How to fix this?
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public static bool bigCube = false;
void FixedUpdate()
{
if (Input.GetKey("w") && PlayerPrefs.GetInt("BigCube", 0) > 0){
if (!onetime1)
{
player.transform.localScale = new Vector3 (2f, 2f, 2f);
rb.mass = 5;
bigCube = true;
forwardForce = 5000;
BigCube();
Invoke("GoBack", 5);
onetime1 = true;
}
}
}
public void BigCube(){
if (!onetime4)
{
PlayerPrefs.SetInt("BigCube", PlayerPrefs.GetInt("BigCube", 0) - 1);
onetime4 = true;
}
}
public void GoBack(){
player.transform.localScale = new Vector3 (1f, 1f, 1f);
bigCube = false; //this line of code doesn't happen when new scene is loaded so player can hit obstacle without dying
rb.mass = 1;
forwardForce = 2000;
}
}
//This is how I used var bigCube to set that you can't die when hit the obstacle if you have big cube
using UnityEngine;
public class PlayerCollision : MonoBehaviour { public PlayerMovement movement; private static bool bigCube;
void OnCollisionEnter (Collision collisionInfo){
if (collisionInfo.collider.tag == "Obstacles"){
if (bigCube == false){
movement.enabled = false;
FindObjectOfType<GameManager>().EndGame();
}
}
}
void Update(){
bigCube = PlayerMovement.bigCube;
}
}
Comment