- Home /
Access a clone of an instantiated object - not all the clones?
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:)
Answer by salsa-project · Jan 12, 2019 at 11:27 AM
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
Answer by badadam · Jan 12, 2019 at 10:36 AM
Add
public bool isLockMove=false; to your car control script.
And then add the code below in the Update method of your car control script which must be top of Update method
private void Update(){
if(isLockMove==true){
return;
}
//your car control
}
And then add trigger collider to your target point and then add script. Make all car tags "car". Your added script to your target point with trigger collider must have this code below
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag=="car")
{
other.gameObject.GetComponent<CarControlScript>().isLockMove = true;
}
}
ty for your answer ... but i found the solution .. i just created a Boolean var .... and i checked if i clicked to Up-Row key is clicked then P = true .... and i add if(p=false){changeliine()} in CarController ... Ty For Your Answer
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers
How do i Instantiate gameObjects in between multiple points? 2 Answers
Moving a object randomly without it being inside a wall 1 Answer
Sudden frame rate drops on mobile when instantiating/destroying? 1 Answer