- Home /
Problem with using Vector3.MoveTowards on randomly instantiated target
In my scene, I have a cube, and this slot that the cube moves towards/snaps into whenever the cube is placed near this collider within the slot. The slots are prefabs that are instantiated at the beginning of each scene in different and randomized positions within the scene. Right now, I can't make the cube move towards the slots ever since turning the slots into prefabs that alternate between different positions.
I have this script attached to the collider within my slot:
public class blockSlot : MonoBehaviour {
private bool correctBlockCubeBlue = false, correctBlockCubeGreen = false, correctBlockCubeRed = false;
public blockCheck check;
private int countCheck;
// Use this for initialization
// Update is called once per frame
void Update () {
if (correctBlockCubeBlue || correctBlockCubeGreen || correctBlockCubeRed) {
check.PlaceBlock();
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "c_blue")
correctBlockCubeBlue = true;
else if (other.tag == "c_green")
correctBlockCubeGreen = true;
else if (other.tag == "c_red")
correctBlockCubeRed = true;
}
}
Which then connects to this script that is attached to my cube:
public class blockCheck : MonoBehaviour {
public Transform target;
public float speed;
public void PlaceBlock()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
This used to work when the slots in my scene were still static and were not prefabs, but since I modified them into being instantiated during every start of a scene, I can't seem to make the cube snap into the slots anymore. I know this has to do with the target transforms, but unfortunately, I'm at a complete loss on what to do.
Where are you assigning Transform target
? You shoud probably assign blockCheck.target in OnCollsionEnter as well.
It's being assigned in the inspector in the prefab themselves. I now know that that won't work because I need to have the target be assigned during its instantiation, but I really don't know what to do next. The problem is the cube can't find it's correct target at all. What do you suggest I do? The cubes are color coordinated (blue cube should go into the blue slot, green cube sjould go into the green slot, etc.) Do I try using tags? Or what?
Answer by hexagonius · Feb 11, 2015 at 10:17 PM
You could have this way easier. Make sure, you instantiate all slots first and all cubes second. Let the cubes look for their slots in Start
public string colorstring = "red";
void Start(){
target = GameObject.Find(colorString + "slot").transform;
}
Let the Cubes start walking by themselves in their Update()
Since this is a game and you wouldn't want the situation where a slot is not present anyway, a null check should not be necessary to make before moving to the target.
Your answer
Follow this Question
Related Questions
Variables stay overwritten in inspector 1 Answer
Instantiate a Prefab as child 0 Answers
Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer
Can't remove instantiated prefab 0 Answers
Destroying & Then Spawning Quickly 1 Answer