- Home /
The question is answered, right answer was accepted
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;
}
}
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.
{
Thanks for your comment. The death zone had the tag "killbox". I fixed it by changing OnCollisionEnter2D to OnCollisionStay2D.
Follow this Question
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