When mouse0 is clicked on a minimap, it instantiates a cube which triggers with the colliders on the minimap to activate a script.
But now I’m trying to figure out how can I only instantiate 1 cube at a time. Meaning, when i click my 1st click, a cube is created. When i click the 2nd time, a new cube is instantiated but the old cube is destroyed. Is there anyway to do it?
I’m using this code.
var hit:RaycastHit = new RaycastHit();
var cloneCube:GameObject;
var MainCam : Camera;
function Update () {
// Instantiation of Waypoints
var ray:Ray = MainCam.ScreenPointToRay(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
if(Physics.Raycast(ray.origin,ray.direction, hit, 1000) Input.GetMouseButtonDown(0))
{
print("clicked on" + hit.point);
var newCube:GameObject;
newCube = Instantiate(cloneCube, hit.point, Quaternion.FromToRotation(hit.normal, Vector3.up));
}
Debug.DrawRay(ray.origin, ray.direction*300, Color.blue);
}
You should avoid instantiating at all, it’s ineffecient as it allocates memory at run time…
But my project requires instantiating of cube which in my case are waypoints. ):
Nobody can help?
Hmm, instantiating multiple copies of a prefab in the way you’re attempting should work just fine. JT is right in that its not super efficient on iPhone, but its definitely an option as long as you’re not doing it excessively.
The only difference between what you’re doing and the way I normally instantiate things is that my Prefab variable is typically a Transform (not a gameobject). Does it work if your newCube/cloneCube vars are Transforms instead? Or perhaps lose the newCube thing altogether and just have the line read:
Instantiate(cloneCube, hit.point, Quaternion.FromToRotation(hit.normal, Vector3.up));
Assigning the result of the instantiation to a variable is strictly optional.
I’m just throwing guesses out, cus I instantiate stuff all the time and it works as expected.
What is the alternative? I use Instantiate extensively.