- Home /
I am not able to reload a Scene a second time for my game
I have not used any static variables, nor have i used any of the DontDestroyOnLOad.
My scripts are as following
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class DragonScript : MonoBehaviour {
public Rigidbody2D myRigidBody;
public Animator myAnimator;
public float jumpForce;
public bool isAlive;
// Use this for initialization
void Start () {
isAlive = true;
myRigidBody = gameObject.GetComponent<Rigidbody2D>();
myAnimator = gameObject.GetComponent<Animator>();
jumpForce = 10f;
myRigidBody.gravityScale = 5f;
}
// Update is called once per frame
void Update () {
isAlive = true;
if (isAlive)
{
if (Input.GetMouseButton(0))
{
Flap();
}
CheckIfDragonisVisibleOnScreen();
}
}
void Flap()
{
myRigidBody.velocity =
new Vector2(0, jumpForce);
myAnimator.SetTrigger("Flap");
}
private void OnCollisionEnter2D(Collision2D target)
{
if(target.gameObject.tag == "Obstacles")
{
isAlive = false;
Time.timeScale = 0f;
}
}
void CheckIfDragonisVisibleOnScreen()
{
if (Mathf.Abs(gameObject.transform.position.y) > 5.4f)
{
isAlive = false;
Time.timeScale = 0f;
}
}
}
above was for flapping dragon animation
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObstacleScript : MonoBehaviour { Rigidbody2D myRigidbody; float dragonXposition; bool isScoreAdded;
GameManagerScript gameManager;
// Use this for initialization
void Start () {
myRigidbody = gameObject.GetComponent<Rigidbody2D>();
myRigidbody.velocity = new Vector2(-2.5f, 0);
dragonXposition =
GameObject.Find("Dragon").transform.position.x;
isScoreAdded = false;
gameManager = GameObject.Find("GameManager")
.GetComponent<GameManagerScript> ();
}
// Update is called once per frame
void Update () {
if(transform.position.x<=dragonXposition)
{
if(!isScoreAdded)
{
AddScore();
isScoreAdded = true;
}
}
if (transform.position.x <= -10f)
{
Destroy(gameObject);
}
}
void AddScore()
{
gameManager.GmAddScore();
}
}
This one is for obstacles
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObstacleScript : MonoBehaviour { Rigidbody2D myRigidbody; float dragonXposition; bool isScoreAdded;
GameManagerScript gameManager;
// Use this for initialization
void Start()
{
myRigidbody = gameObject.GetComponent<Rigidbody2D>();
myRigidbody.velocity = new Vector2(-2.5f, 0);
dragonXposition =
GameObject.Find("Dragon").transform.position.x;
isScoreAdded = false;
gameManager = GameObject.Find("GameManager")
.GetComponent<GameManagerScript>();
}
// Update is called once per frame
void Update()
{
if (transform.position.x <= dragonXposition)
{
if (!isScoreAdded)
{
AddScore();
isScoreAdded = true;
}
}
if (transform.position.x <= -10f)
{
Destroy(gameObject);
}
}
void AddScore()
{
gameManager.GmAddScore();
}
}
This is game manager
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class MenuCtrl1 : MonoBehaviour {
// Use this for initialization
public void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
and last one is to change scenes
please correct the codes where possible