- Home /
Scripted Boundaries Not Working Correctly
Hello, today I decided to make a "Block Dodger" game. Currently, my Physics 2D engine is not working as I want it to work. So to block the player from going out a region its not supposed to, I decided to use code to block it. I came up with this:
using UnityEngine; using System.Collections;
public class Collider : MonoBehaviour {
void Update () {
if (transform.position.x < 6)
{
transform.position -= 1;
print ("ColliderRight Has Been Activated");
}
if (transform.position.x < -6)
{
transform.position.x += 1;
print ("ColliderLeft Has Been Activated");
}
}
}
In this code, I access the "transform.position" component of my player. Then I do if this x position is bigger than 6, then the code -1 the x position.
But there is a problem. I assign the script to the player, then run it, then it says:
Please fix any scripting errors before playmode! (Not exactly that but something alike).
I have no idea what to do. I spent 3 hours looking for solutions but there just isn't. Am I doing something wrong?
Please help me.
-Cryptosan