- Home /
The question is answered, right answer was accepted
Instantiate Objects on Cells created during run time
Hey there, I created a grid:
using UnityEngine;
using System.Collections;
public class Grid : MonoBehaviour {
public Transform CellPrefab;
public Vector3 Size;
// Use this for initialization
void Start () {
CreateGrid ();
}
// Update is called once per frame
void CreateGrid () {
for (int x = 0; x < Size.x; x++) {
for (int z = 0; z < Size.z; z++) {
Instantiate (CellPrefab, new Vector3 (x, 0, z), Quaternion.identity);
}
}
}
}
And I want to create cubes on top of the cells when i click them , therefor i used a raycasthit and the name of the hit object. The Problem i have is that im somehow not able to click on the cells. It works if i use a Plane and try to click on it but not on the cells.
void Update () {
if (Input.GetMouseButtonDown (0)) {
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Debug.Log("1");
if (Physics.Raycast(ray, out hit) && hit.transform.name=="Cell"){
//Instantiate (CannonPrefab);
Debug.Log("hit");
}
}
}
Im aware that the current script does not actual would spawn each cannon in each cell of the grid but i dont want to go on while i even cant click on the cells.
Does anyone have a clue why it wont work like this?
Are you spawned Cells actually named "Cell" or are they Cell(clone), Cell(clone), Cell(clone) since you aren't rena$$anonymous$$g your instantiated gameobject
As @getyour411 indicates, they will have the name "Cell (clone)". You might want to use a tag ins$$anonymous$$d, or you can name them explicitly in your creation code. Plus if your cells are a mesh you've imported, you will have to add a collider of some sort to them.
Answer by CKM92 · Jan 11, 2014 at 01:33 AM
omg^^
void CreateGrid () {
for (int x = 0; x < Size.x; x++) {
for (int z = 0; z < Size.z; z++) {
Object cl = Instantiate (CellPrefab, new Vector3 (x, 0, z), Quaternion.identity);
cl.name = "Cell";
}
}
}
works now thx everybody
Follow this Question
Related Questions
Grid Placement In Game 2 Answers
how to tell if fence encloses area 1 Answer
raycast and instantiat advice 0 Answers
Instantiating Object On Map 1 Answer
How to prevent GameObject from spawning on top of each other? 1 Answer