if button help?

var towerGhost : Transform;
var towerActual : Transform;
var gridPoint : Vector3;

function Update () {
    var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var ghostTower : Transform;
	
	  if ( Physics.Raycast( ray, hit, 100 ) ){
    	
    	if (hit.collider.CompareTag("gridFloor")){
    		
    		gridPoint = hit.collider.transform.position;
    		
    		
    		if(gridPoint != null){
    			
    			if(Input.GetMouseButtonDown(0)){
    				ghostTower = Instantiate(towerGhost,gridPoint,Quaternion.identity);
    			}
    			if(Input.GetMouseButton(0)){
    				ghostTower.position = gridPoint;
    				}
    			}
    			if(Input.GetMouseButtonUp(0)){
    				Instantiate(towerActual,gridPoint,Quaternion.identity);
    			}
    					
    			
    				}
    			
    			}
			}

does anybody know why this wouldn’t be working?

both ghost tower and gridpoint have been assigned and are showing up in debug.log i dont get it

Update: ok so i posted my entire script hoping it would make more sense now.

OHHHH!!! I know what your problem is now.

You are defining your variable ‘ghostTower’ in the wrong scope! You need to put it at the top of your script, where it won’t get overwritten every time Update gets called! The way it is a the moment, it’ll never get saved between frames. I remember the first part of this question, and I was really racking my brains to work out what was going wrong, but now that I’ve seen your whole script, it makes sense.

Just make it private so it doesn’t show up in the inspector, but put it at the top next to your prefab declarations. Then, you’ll be able to reference it beyond the frame when you spawned it!

Try taking the GameObject out.

if(Input.GetMouseButton(0)){
     ghostTower.transform.position = gridPoint;
}