0
\$\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\$
  • \$\begingroup\$ Could you please share some code of how exactly you create the cars from prefab and put them into the scene? \$\endgroup\$ – real4x Jan 12 at 10:44
  • \$\begingroup\$ ooh sry .. i will \$\endgroup\$ – Mr Salsa Jan 12 at 11:09
  • \$\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 Jan 12 at 11:37
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\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.