Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 question was closed Dec 01, 2017 at 05:31 AM by McYellowBird for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by McYellowBird · Nov 30, 2017 at 10:32 AM · 2dgameobject2d-platformerplatformerdeath

2D Platformer - Problem with object that kills, then restarts after build

Hiya. I'm new to Unity and C# and have been following a youtuber's tutorials on making a 2D endless runner game. In Unity, everything works perfectly, but when I build it. the 'deathzone' doesn't work. I built it with developer version and this is what comes up:

 UnityException: GameObject has undefined tag!

I don't know how I'm meant to fix it. The code is below, or here: http://collabedit.com/42yp8

For the player controller:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Controls : MonoBehaviour {
 
     public float moveSpeed;
     private float moveSpeedStore;
     public float speedMultiplier;
 
     public float speedIncreaseMilestone;
     private float speedIncreaseMilestoneStore;
 
     private float speedMilestoneCount;
     private float speedMilestoneCountStore;
 
     public float jumpForce;
 
     public float jumpTime;
     private float jumpTimeCounter;
 
     private Rigidbody2D myRigidbody;
 
     public bool grounded;
     public LayerMask whatIsGround;
 
     private Collider2D myCollider;
 
     private Animator myAnimator;
 
     public GameManager theGameManager;
 
     // Use this for initialization
     void Start () {
         myRigidbody = GetComponent<Rigidbody2D>();
 
         myCollider = GetComponent<Collider2D>();
 
         myAnimator = GetComponent<Animator>();
 
         jumpTimeCounter = jumpTime;
 
         moveSpeedStore = moveSpeed;
         speedMilestoneCountStore = speedMilestoneCount;
         speedIncreaseMilestoneStore = speedIncreaseMilestone;
     }
 
     // Update is called once per frame
     void Update() {
 
         grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
 
         if(transform.position.x > speedMilestoneCount)
         {
             speedMilestoneCount += speedIncreaseMilestone;
 
             speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;
 
             moveSpeed = moveSpeed * speedMultiplier;
         }
 
         myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
 
         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetMouseButtonDown(0))
         {
             if (grounded)
             {
                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             }
         }
 
         if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetMouseButton(0))
         {
             if(jumpTimeCounter > 0)
             {
                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
                 jumpTimeCounter -= Time.deltaTime;
             }
         }
 
         if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.W))
         {
             jumpTimeCounter = 0;
         }
 
         if(grounded)
         {
             jumpTimeCounter = jumpTime;
         }
 
         myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
         myAnimator.SetBool("Grounded", grounded);
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "killbox")
         {
             theGameManager.RestartGame();
             moveSpeed = moveSpeedStore;
             speedMilestoneCount = speedMilestoneCountStore;
             speedIncreaseMilestone = speedIncreaseMilestoneStore;
         }
     }
 }
 
 
 
 
 
 
 
 
 
 
 
 

Game Manager

 using System.Collections.Generic;
 using System.Collections;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameManager : MonoBehaviour {
 
     public Transform platformGenerator;
     private Vector3 platformStartPoint;
 
     public Controls thePlayer;
     private Vector3 playerStartPoint;
 
     private PlatformKiller[] platformList;
 
     private ScoreManager theScoreManager;
 
     // Use this for initialization
     void Start ()
     {
         platformStartPoint = platformGenerator.position;
         playerStartPoint = thePlayer.transform.position;
 
         theScoreManager = FindObjectOfType<ScoreManager>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         
     }
 
     public void RestartGame()
     {
         StartCoroutine("RestartGameCo");
     }
 
     public IEnumerator RestartGameCo()
     {
         theScoreManager.scoreIncreasing = false;
         thePlayer.gameObject.SetActive(false);
         yield return new WaitForSeconds(0.5f);
         platformList = FindObjectsOfType<PlatformKiller>();
         for(int i = 0; i < platformList.Length; i++)
         {
             platformList[i].gameObject.SetActive(false);
         }
 
         thePlayer.transform.position = playerStartPoint;
         platformGenerator.position = platformStartPoint;
         thePlayer.gameObject.SetActive(true);
 
         theScoreManager.scoreCount = 0;
         theScoreManager.scoreIncreasing = true;
     }
 }




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

1 Reply

  • Sort: 
avatar image
0

Answer by mafima · Nov 30, 2017 at 01:40 PM

you just need to give the collider object the correct tag. You may only set the tag for the parent of the collider object.

   void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "killbox") // error here probably. is the tag really on the object with the collider? maybe its the parent or child.
         {

Comment
Add comment · Show 1 · Share
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
avatar image McYellowBird · Dec 01, 2017 at 05:31 AM 0
Share

Thanks for your comment. The death zone had the tag "killbox". I fixed it by changing OnCollisionEnter2D to OnCollisionStay2D.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character animation jerks when moving 1 Answer

jittery collisions 2d platformer 1 Answer

2d Platformer sprites background 1 Answer

2d collider stuck issue 1 Answer

Sprites and Platforming? 1 Answer


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