Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

One way ignoring collision 2 Answers

Why my sound it's not attaching to my Flashlight 2 Answers

Problem with First Person Shooter Script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

problem with my simple script please help. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges