control amount of instantiated objects in getmousebutton

does anybody have any ideas on how to control an instantiated objects count? i am lost and could use some help please.

if(Input.GetMouseButton(0)){

    				var thisTower = Instantiate(tower1,gridPoint,Quaternion.identity);
    				thisTower.transform.position = gridPoint.position;
    				}

1 Answer

1

EDIT- I'm afraid I didn't really understand your question to begin with, but now I've got a much better answer for you!

private var ghostTower : Transform;

function Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        ghostTower = Instantiate(transparentTower,gridPoint.position,Quaternion.identity).transform;
        ghostTower.transform.position = gridPoint.position;
    }
    if(Input.GetMouseButton(0))
    {
        if(ghostTower != null)
            ghostTower.position = gridPoint.position;
        }
    }
    if(Input.GetMouseButtonUp(0))
    {
        Destroy(ghostTower);
        Instantiate(realTower, gridPoint.position, Quaternion.identity);
    }
}

This has the added advantage of preventing the 'ghost towers' from attacking while they are being placed- since the ghost towers aren't actual towers, they don't target things when you are dragging them around. This prevents at least one gameplay exploit!

EDIT I’m not all that clear on exactly how Instantiate works in Javascript- because of the weak typing, I’m not sure exactly what gets returned. I assume it just guesses based on they type you are assigning to, but I’m not completely certain.

well what i want is to instantiate a tower after i hit the mouse button and to do that dont i have to put it in the update function? so what i was thinking that if i could control how many are instiantiated i could make it so that they dont spawn every frame...... so how would i make it so that it only makes on of of that counter, would something like this work?? if(Input.GetMouseButton(0)){ if(towerCount== 0) instantiate...... towerCount++; } for some reason the logic doesnt make sense and i have tried that way with booleans but it wont work? any more ideas?

ps Sweet answer time!!! ha ha that was like 4 min after i asked!!

Oh I see. You need to either use OnMouseButtonDown, so that it only happens in the frame that you start clicking the mouse, or have some kind of system where when a tower is spawned in a grid point, no more towers can spawn there. As for the answer time, that's what happens when I'm procrastinating.

i tried it with OnMouseButtonDown, but i also need it to translate to my current gridPoint while the click is still going and stay there when i release it, down doesn't allow me to do that!

no i just have my translation under my OnMouseButtonDown so as soon as i hit it its already done translating, then i have another problem with OnMoustButton where i instiantate on everysingle grid i put my mouse over, so i either need to translate it while using OnDown or i need to control the amount in OnMouseButton. Hope this make more sense to my situation!