- Home /
Transform does not move GameObject
Hi, I'm pretty new to c# and unity so please bare with me. I want to transform.position a Gameobject on a grid by using collider.bounds.center as a vector 3 reference. I am trying to do this through a tilescript which equals one tile in an array. When I instantiate the GameObject/PlayerPrefab I am able to clone the Gameobject onto each tile (which is what I want to do with the transform.position). However when I try to transform.position the GameObject nothing works.
public class TileScript : MonoBehaviour {
public Vector3 tileCenter;
public GameObject PlayerPrefab;
public PlayerScript Select;
public float x,y,z;
// Use this for initialization
void Start () {
//tileCenter = new Vector3 (x, 3, z);
x = this.transform.collider.bounds.center.x;
y = this.transform.collider.bounds.center.y;
z = this.transform.collider.bounds.center.z;
}
void Update () {
}
void OnMouseEnter() {
gameObject.transform.renderer.material.color = Color.blue;
}
void OnMouseExit() {
transform.renderer.material.color = Color.white;
}
public void OnMouseDown()
{
PlayerPrefab.transform.position = new Vector3 (x, 3, z);
//if (Select.selected == true)
//var Move = Instantiate(PlayerPrefab, new Vector3 (x,0,z), Quaternion.AngleAxis (0,Vector3.right));
//tileCenter = newPos;
//print (x);
}
}
I have tried multiple variations of transform.position, none seem to work. Any help would be appreciated, thanks.
If player prefab is a prefab you would need to instantiate it first.
Thankyou for your response. You mean having a prefab already dragged out onto the scene does not count as a Gameobject that I can move?
You can set tranform's position after its instantiation, but not before.
public void On$$anonymous$$ouseDown() { if (Select.selected == true) var $$anonymous$$ove = Instantiate(PlayerPrefab, new Vector3 (x,0,z), Quaternion.AngleAxis (0,Vector3.right)); $$anonymous$$ove.transform.position = new Vector3 (x, 3, z); // moving tileCenter = newPos; print (x); }
"You mean having a prefab already dragged out onto the scene does not count as a Gameobject that I can move?"
The confusion is about prefabs. A true prefab does not exist in the scene. It is a blueprint for an object.
https://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html
As for your issue, your code should work if you are clicking on the object and there are no other colliders between the camera and the object. On$$anonymous$$ouseDown() only fires if you click on the specific object that has that callback.
Thanks for all the feedback. I think leaving in the commented code has added to the confusion. What I'm trying to say is that I want Transform.position to do what Instantiate is doing but moving the object ins$$anonymous$$d of cloning it like what transform.position is supposed to do. Your explanation has kind of made me understand why instantiate works and transform.position does not. Thanks I'll keep banging away at it and let you know how I go.
Answer by Kavman · Mar 09, 2014 at 05:39 PM
Ok I got it to work by instantiating the prefab on the grid creation script and transform.position in the player script on Input.mouseButtonDown (0)). Using Raycast though but atleast it works. Thanks for the input.
Your answer
Follow this Question
Related Questions
GUI Buttons and game objects? 1 Answer
Moving a GameObject to a certain point in world space via script 1 Answer
Instantiate, problem on transform position 1 Answer
How do I create roadblocks in my Match 3 puzzle gameboard? 2 Answers
Help with C# - destroy at a certain position not working 0 Answers