- Home /
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
Answer by syclamoth · Oct 09, 2011 at 10:55 PM
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!
Answer by MrSplosion · Oct 09, 2011 at 10:17 PM
Try taking the GameObject out.
if(Input.GetMouseButton(0)){
ghostTower.transform.position = gridPoint;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613002610im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to move or fade GUITextures with iTween 2 Answers
How do you make an object go left and right? 1 Answer
Top-Down Camera Trouble 1 Answer
walking around a spheroid planet centered at the origin 1 Answer
Make Explosion Child of Target 1 Answer