1
\$\begingroup\$

I am trying to access the object I instantiate - basically a prefab car instantiates cars (one after one) ..the problem that when try to move one .. they all move together ... what's the best way to separate them (and control them one after one)

the game is about parking .. the first car appear i choose which line to move to (target = empty-object) , then the second car appears and i choose which line also to move (the problem is here : when i hit the left/right-row all the cars go left and right .. i want only the last one to change the line )!! Any Ideas ... Thank You:)

this in Spawn.cs

public void SpawnCars()
{
    if (spawnAllowed)
    {
        // choose a random strat point 1/6     
        randomSpawnPoint = Random.Range(0, spawnPoints.Length);
        // choose a random car 1/6
        randomSpawnCars = Random.Range(0, Cars.Length);
        // clone randomly a car(1/6) from a random pre_set-points(1/6)
        GameObject obj = Instantiate(Cars[randomSpawnCars], spawnPoints[randomSpawnPoint].position, Quaternion.Euler(-90, 180, 0)) as GameObject;
        // carNumber will count how many cloned car 
        carNumber++;
        // destroy the  cloned car after N sec
       // Destroy(obj, 8);
        // every cloned car will take a tag with it
        obj.gameObject.tag = "select" + k;
        k++;
        rcf = obj.GetComponent<RayCastForward>();

    }
}

this in CarController.cs changing line problem (it change all the cars line)

void ChangeLine()
{        
     if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        f.RayDis();
        if (f.v != "R6")
            {
                tf.transform.position = new Vector3(tf.position.x,tf.position.y,tf.position.z - 6.5f);
            }                            
    }
    else if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        f.RayDis();
        if (f.v != "R1")
            {
                tf.transform.position = new Vector3(tf.position.x, tf.position.y, tf.position.z + 6.5f);
            }            
    }

the car will move to target after i click the UpRow key

// move the car to (target)
public void MoveCars(string go)
{        
        target = GameObject.Find(go);
        directionToTarget = (target.transform.position - transform.position).normalized;
        rb.velocity = new Vector3(directionToTarget.x * moveSpeed, directionToTarget.y * moveSpeed, directionToTarget.z * moveSpeed);        
}
\$\endgroup\$
3
  • \$\begingroup\$ Could you please share some code of how exactly you create the cars from prefab and put them into the scene? \$\endgroup\$
    – real4x
    Commented Jan 12, 2019 at 10:44
  • \$\begingroup\$ ooh sry .. i will \$\endgroup\$
    – Mr Salsa
    Commented Jan 12, 2019 at 11:09
  • 1
    \$\begingroup\$ To be clear: your problem here is that you're running a copy of the input-handling script on every instance of the car, rather than just once for the car you want to control. Strategies to deal with this include marking the selected car in some way and checking "Am I the marked car?" before processing input, disabling/removing the input-handling script from cars that aren't supposed to be processing input, or putting the input-handling script on a (singular) external controller object that manipulates the selected car. \$\endgroup\$
    – DMGregory
    Commented Jan 12, 2019 at 11:37

2 Answers 2

1
\$\begingroup\$

It appears like each of your cars has its own CarController component. So every car is controlled separately.

There are several ways to handle this issue:

  • When you change your car into the "parked" state, remove its CarController with Destroy(GetComponent<CarController>());
  • Disable the CarController with GetComponent<CarController>().enabled = false;. When the property enabled of a MonoBehaviour is set to false, then the engine will no longer call its Update method. This is preferable to destroying the component if you want to have the option to re-enable control of the car or if that component also carries some data or functionality you still need even if the car is parked.
  • But maybe your CarController does more than just input handling and also contains code in its Update method which is supposed to be executed for parked cars? In that case you could add a variable isControlled to the CarController. Set it to false when the car changes into the parked state. Wrap the input handling in Update in a if (isControlled) { block.
  • Separate the CarController from the Car game object it controls. Create an invisible game object in your scene. Give that object a CarController script. But modify that script so it doesn't control its own game object. Give it a property public GameObject controlledCar representing the car it is currently controlling. When you spawn a new car, change the controlledCar of your carController.
\$\endgroup\$
0
\$\begingroup\$

I found the solution ..

1/ create boolean var:

public bool u;

2/put the changeline function in if condition:

void Update() {
    if (u)
    {
        ChangeLine();
    }

3/ and u will be true if i pressed the Up-Row key (UpRow key will tell the car to move to target)

 if (Input.GetKeyDown(KeyCode.UpArrow))
    {   u = false;   
        moveCar();
    }       

Thank for your time guys

\$\endgroup\$
1
  • \$\begingroup\$ u is a very bad name for a variable. What exactly does it represent? \$\endgroup\$
    – Philipp
    Commented Oct 9, 2019 at 13:21

You must log in to answer this question.

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