0
\$\begingroup\$

Ok so ive created a prefab of an obstacle avoidance bot and am attempting to clone it. Problem is, only one of the instances works correctly, the rest just sit there. The bots are supposed to turn left when right sensor is triggered and vice versa. The instantiation code:

public int populationSize = 10;
public GameObject carPrefab;
public Vector2 spawnPosition;
public static float speed = 2f;


private GameObject[] cars;

// Use this for initialization
void Start () {
    cars = new GameObject[populationSize];
    for (int i = 0; i < populationSize; i++) {
        spawnPosition = new Vector2((1.0f*i)-2,3.83f);
        cars [i] = GameObject.Instantiate (carPrefab,spawnPosition,Quaternion.identity);
    }

}

// Update is called once per frame
void Update () {

}

The prefab scripts: (Left Sensor and Right Sensor are children of CarMovement)

public float speed = 2f;
public static Rigidbody2D rb2d;

// Use this for initialization
void Start () {
    rb2d = GetComponent<Rigidbody2D> ();

}

// Update is called once per frame
void Update () {
    rb2d.transform.Translate (Population.speed * Time.deltaTime, 0, 0);
}

Left Sensor:

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerStay2D (Collider2D other){
    Debug.Log ("LEFT");
    CarMovement.rb2d.rotation -= 5;
}

Right sensor:

Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RightSensor : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerStay2D (Collider2D other){
    Debug.Log ("RIGHT");
    CarMovement.rb2d.rotation += 5;
}

Any help would be greatly appreciated. I have no idea whats going on.

\$\endgroup\$
2
  • \$\begingroup\$ What is the Population.Speed? And you should apply force to the rigidbody if you want it to move instead of translating. \$\endgroup\$ Commented Aug 30, 2017 at 9:24
  • \$\begingroup\$ Have you checked none of the scripts/components of the GameObjects have lost any reference or configuration? \$\endgroup\$ Commented Aug 30, 2017 at 10:52

1 Answer 1

1
\$\begingroup\$

Why did you choose to define rb2d as static?

public static Rigidbody2D rb2d;

This tells the compiler to allocate only one memory location for this variable, no matter how many instances of the class you have. So they all end up sharing a single Rigidbody2D variable.

Whichever instance happens to run its Start() method last will assign its Rigidbody2D to this shared variable, overwriting whatever the other instances had tried to store there.

From that point onward, every reference to CarMovement.rb2d will refer to only this last instance's Rigidbody2D, moving it and ignoring everyone else.

If you're looking for a way for one instance of a script to influence just one instance of another, a static variable is not an appropriate solution.

It looks like what you really want is for your sensor class to look like this:

public class Sensor : MonoBehaviour {

    // Give each one an instance variable you can wire up in the Inspector
    // to point to the CarMovement or Rigidbody2D you want this sensor to affect.
    public Rigidbody2D body;

    // While we're at it, store the rotation rate as an instance property too, so
    // we can do both left and right movement with the same script, and tune the speed.
    public float avoidance = 5f * 50f;

    void OnTriggerStay2D (Collider2D other){
        Debug.Log (avoidance < 0f ? "LEFT" : "RIGHT");
        // Storing avoidance in degrees per second and scaling by deltaTime
        // means you won't have to re-tune all your behaviours if you ever
        // change your FixedUpdate timestep.
        body.rotation += avoidance * Time.deltaTime;
    }    
}

When you spawn an instance of this sensor as part of the prefab containing the Rigidbody2D it points to, the body variable above will automatically point to the Rigidbody2D spawned as part of this instance.

If the sensors aren't part of the same prefab as the body they modify, then you can wire up these dependencies dynamically on spawn using the strategies discussed in this answer.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks! It works great now! Is there a way to make it so the instances of the car prefab can't interact with one another. They are bumping into each other. \$\endgroup\$
    – iamPres
    Commented Aug 30, 2017 at 19:19
  • \$\begingroup\$ That's a different question. You want to look at physics layers. \$\endgroup\$
    – DMGregory
    Commented Aug 30, 2017 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .