- Home /
Script controls 2 game objects not independently
I've recently started unity, so as a sort of exercise I wanted to make a remake of a Mario level. The part where i got stuck is when I made patrolling Goombas. It may be an inefficient and basic method, but essentially the idea is that I made them reverse direction when hitting another object. The problem is, that for some reason, when one hits a wall, both of them reverse direction. The script is, of course, applied to both.
using UnityEngine;
using System.Collections;
public class GoombaMove : MonoBehaviour {
public static float speed = -0.01f;
// Update is called once per frame
void Update () {
this.transform.Translate(speed, 0, 0);
}
void OnTriggerEnter2D(Collider2D other)
{
speed *= -1;
}
}
I'm at a bit of a loss here...
Answer by Rob2309 · Aug 24, 2016 at 07:02 PM
the problem is that your speed variable is static, which means its not bound to the object you are manipulating. Just removing the static token should fix the issue :)
Your answer
Follow this Question
Related Questions
How To Stop Movement With RigidBody.AddForce 2 Answers
Multiple Cars not working 1 Answer
What is the best way to update array in runtime? 3 Answers
Distribute terrain in zones 3 Answers
Trigger Sound more than Once 0 Answers