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.

gridPoint is a Vector3, no? If ghostTower is a transform, you really don't need the .gameObject.transform bit to refer to itself...

–

i guess i should add that the object i am trying to move has just been instantiated at gridPoint and now it wont track to my current grid point, and i just deleted the gameObject and transform, but still no cigar

–

2 Answers

2

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!

Thank you dude! i guess next time i should just post my entire script ha ha i appreciate all the help!

–

Try taking the GameObject out.

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

still wont work

–

ok, well like syclamoth said is gridPoint a Vector3?

–

yes, sorry forgot to add that to

–