- Home /
Instantiate same PreFab to an array of GameObject by RayCast.
Hi guys, I need to put a Prefab in to an array of GameObject and make them look at the next one so i declare this:
var prefab: GameObject;
var someObject: GameObject[];
I know how to use Raycast to instantiate a single object like this:
function Update()
{
var hit: RaycastHit;
var ray = Cam.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
if (Physics.Raycast(ray, hit))
if (hit.collider.name == "Quad2")
GameObject = Instantiate(prefab, hit.point, Quaternion.identity);
And use transform.LookAt() to make transform look at something. But i don't know how to instantiate to an array and make them look at and caculate distance to the next one in array and how to destroy the last one in an array. I really need it.
Thanks in advance.
Answer by PaulUsul · Jan 21, 2014 at 03:14 PM
I would suggest changing to C# and using a list, then you can add to it and look at Count-2 for the previous or take the previous and look at the one you instantiated(There is probably a js list).
This is very basic programming, maybe read some tutorials on array usage. How many does there need to be?
..
int i = 1;
..
Start(){
// array with 20
someObject = new GameObject[20];
}
..
..
// put the instantiated gameObject into the i'th position.
someObject[i] = Instantiate(prefab, hit.point, Quaternion.identity);
// check that there is something that can look at us.
if(i - 1 >= 0){
someObject[i - 1].transform.LookAt(someObject[i].transform.position);
}
i++;
Something along those lines.