- Home /
Help - Instantiate A Prefab On Mouse Position
This has been bugging me for ages now! I honestly don't know what to do (besides Unity Answers).
Right, I have a 'click and drag' system going on - its basically a GUI button, in which when you click on it a 'dummy turret' prefab appears under your mouse, and where ever you click the 'dummy turret' turns into a 'real turret' prefab and is placed
dummy = it has no script - so it wont fire at enemy when positioning turret.
real = it has script - it will shoot!
Now! What I am TRYING to do, is not allow the user to place a turret on a collider tagged 'NoZone' (the path in which the enemy walk on). I have also made another prefab for this which is basically a 'dummy' one but with red material applied. This is what I want to be instantiated under the mouse instead of the dummy when user is raycasting over NoZone ... and vice verser.
Here is my DragScript:
var objNoZone : Transform; // the prefab used for a no zone area - (not allowed)
var objDummy : Transform[]; // the prefab that needs to be initiated first var objGroup : Transform[]; // the prefab that is initiated when the user placed the dummy
var objItemNames : String []; var objLimit : int [];
var selGridInt : int = 0;
private var selectedGrid : int = 0; var groundPlane : Transform;
var dummyObj : Transform;
var currentObj : Transform;
var cloneHolder : Transform; var followSpeed : float = 10; private var chooseLocation : boolean = false; private var cancelSelection : boolean = false; var currentSelected : int = 30;
var gunTurretGUI : Texture; var missileTurretGUI : Texture;
private var ZoneHit : boolean = false; private var ZoneOn : boolean = false; private var currentHit;
function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; var layerMask = 1 << 8; if (chooseLocation) { layerMask = ~layerMask; Physics.Raycast (ray, hit, 600, layerMask); Debug.DrawLine (ray.origin, hit.point); dummyObj.Translate((Vector3(hit.point.x, dummyObj.position.y, hit.point.z) - dummyObj.position) * Time.deltaTime *followSpeed); currentHit = hit;
}
if(currentHit.collider.tag == "NoZone" && ZoneOn == false){
objNoZone = Instantiate (objNoZone,Vector3(0,0,0),Quaternion.identity);
objNoZone.Translate((Vector3(dummyObj.position.x, dummyObj.position.y, dummyObj.position.z) - currentObj.position));
//ZoneOn = true;
}
if (Input.GetButtonDown("Fire1") && chooseLocation == true && ZoneHit == false){
currentObj = Instantiate (objGroup[currentSelected],Vector3(0,0,0),Quaternion.identity);
currentObj.Translate((Vector3(dummyObj.position.x, dummyObj.position.y, dummyObj.position.z) - currentObj.position));
chooseLocation = false;
Destroy(dummyObj.gameObject);
}
// Cancel Selection \\
if(Input.GetButtonDown("Fire2") && dummyObj.gameObject){
objLimit[currentSelected] ++;
chooseLocation = false;
//Destroy(currentObj.gameObject);
Destroy(dummyObj.gameObject);
}
}
function OnGUI () {
selGridInt = 0;
if(chooseLocation == false){
//if (GUI.Button(Rect(10, 10, 120, 30), objItemNames[selGridInt] + " ("+objLimit[selGridInt]+")") && objLimit[selGridInt] > 0){
if (GUI.Button(Rect(100, Screen.height - 170 ,140 ,140), gunTurretGUI)){
currentSelected = selGridInt;
chooseLocation = true;
objLimit[selGridInt] --;
dummyObj = Instantiate (objDummy[selGridInt],Vector3(0,0,0),Quaternion.identity);
dummyObj.position.y = groundPlane.position.y + 0.5;
selectedGrid = 0;
}
}
}
Now what happens so far, is that the 'NoZone dummy' keeps initiating at 0,0,0 every frame whilst the mouse is over the collider NoZone.
Pleaseeee help me! Would much appreciate it!
Answer by Bampf · Apr 01, 2011 at 01:22 PM
The code that is instantiating the NoZone dummy doesn't have the same IF conditions that the other IF-clauses do, so it is running too often. In particular, it is running when chooseLocation is false.
For starters, it should check for chooseLocation == true:
if(currentHit.collider.tag == "NoZone" && chooseLocation == true && ZoneOn == false){
Most of the Update code only runs when chooseLocation is true, so it might be clearer to just check that once.
Another issue is that you don't seem to be destroying objNoZone anywhere.
Instead of two models, an easier approach would be to just change a texture of the turret's material, depending on which zone the player is mousing over. (Or you could swap in an entirely different material if you prefer.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613120138im_/https://answers.unity.com/themes/thub/images/avi.jpg)