- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Raditya_Ahi · Dec 19, 2014 at 11:10 PM ·
GameState Script Problem
Hi, i'm just a newbie who develop a 2d infinite runner game. i was following a tutorial and remade with my own asset. I got a problem when make the GameState script. here the script
using UnityEngine;
using System.Collections;
public class charController : MonoBehaviour {
Animator anim;
public Vector2 jumpForce = new Vector2 (0,300);
public Transform groundCheck;
bool grounded = false;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public Collider2D JumpButton;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
//handle back key in Windows Phone
if (Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
if (GameStateManager.State == State.Intro)
{
if (WasTouchedOrClicked())
{
GameStateManager.State = State.Play;
}
}
if ((grounded) && WasTouchedOrClicked() && GameStateManager.State == State.Play)
{
Vector2 contactPoint = Vector2.zero;
if (Input.touchCount > 0)
contactPoint = Input.touches[0].position;
if (Input.GetMouseButtonDown(0))
contactPoint = Input.mousePosition;
if (JumpButton == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(contactPoint)))
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
anim.SetBool ("Ground", false);
}
}
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Obstacle")
{
GameStateManager.State = State.Dead;
deadMoves();
}
}
bool WasTouchedOrClicked()
{
if (Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) ||
(Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Ended))
return true;
else
return false;
}
void deadMoves()
{
anim.SetBool ("dead", true);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public enum State
{
Intro,
Play,
Dead
}
public static class GameStateManager
{
public static State State { get; set; }
static GameStateManager ()
{
State = State.Intro;
}
}
using UnityEngine;
using System.Collections;
public class obstacleSpawner : MonoBehaviour
{
// a public object array for which objects to spawn
public GameObject[] obj;
//min and max times for another spawn
public float spawn = 2f;
void Start () {
//start spawn
Spawn();
}
void Spawn()
{
if (GameStateManager.State == State.Play)
{
//get random number
float rand = Random.Range (1, 4);
if (rand > 2 && rand < 4)
{
Instantiate (obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
}
//invoke spawn at random time interval between min and max
Invoke ("Spawn", Random.Range (2, 2));
}
}
}
The problem is the obstacle didn't spawn, but the character can die. please help me and explain it. thank you
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613174428im_/https://answers.unity.com/themes/thub/images/avi.jpg)