- Home /
I tried to transform the position of the clone of a prefab but it doesnt move.
In the first place thanks, and i have to say im new into unity.
whenever i click on a block(from a board, like a board game) i want the player to move to that block, but it doesnt move.
here´s the method that should move the player(a.k.a chip)
public void Moving(float x, float z)
{
chip.transform.position = new Vector3( x, 0f, z);
Debug.Log(x + " " + z);
}
eventhough i click on a block and it shows in the console the right position, the player doesnt move. one mmore thing, i dont know if its relevant but the player prefab has a "playerGraphic" sprite as child
Are you sure the transform doesn't move? Click on the player main game object in scene hierarchy so the transform will have axis arrows in editor window, then launch the game, click on the tile and check player's game object position.
Unity has some bugs with sprites, in my case it was sprite object which I was trying to use as a pivot for other objects and it turned out that sprite positions were inadequate when I tried to move it. $$anonymous$$aybe, your sprite doesn't move while the player root gameobject itself actually moves.
Answer by rainChu · Aug 24, 2018 at 06:06 AM
First of all, welcome to Unity!
Your code seems to be correct. I think that's why this question lasted so long without an answer to it. I'm not sure what is going wrong without seeing the rest of your program, the most I can do is provide general advice for tracking down the culprit. What is chip
? You should run the game in the editor, using the play button, and look at the actual values of the Transform component. Did they change? If not, can you verify that chip
is a reference to the Component or GameObject you think you're affecting? Another scenario that happens to me sometimes: I change the position of something, and something else I coded and then forgot about changes it back. If this is so, you can write a debug statement in the Update()
event on the object you want to affect, and see what the position is each frame.
Good luck tracking it down!
thanks for answering! (the chip is like the king on chess) the chipPrefab has the chipScript wich is this one. and it references the chipPrefab aswell(in the inspector) I´ve also tried to put "this.transform" or simple "transform" but ot has same result
public class chipScript : $$anonymous$$onoBehaviour {
public GameObject chip;
void On$$anonymous$$ouseDown()
{
}
public void $$anonymous$$oving(float x, float z)
{
chip.transform.position = new Vector3( x, 0f, z);
Debug.Log(x + " " + z);
}
}
here´s the "block script" too
public GameObject tile;
public chipScript chipScript;
private float tileX;
private float tileZ;
void Start ()
{
tileX = tile.transform.position.x;
tileZ = tile.transform.position.z;
}
void On$$anonymous$$ouseDown()
{
chipScript.$$anonymous$$oving(tileX, tileZ);
}
and i´ve checked the coordinates in run time but they dont change neither.( And when I manually put something like 1,0,0 it moves) and again, thank you for answering!
So what object are you assigning to chip
? I want to be sure you understand how Unity works. this.transform
and transform
are equivalent when you're inside a $$anonymous$$onoBehaviour
. It's the Transform on the same GameObject as your component. Your script looks fine at first glance, which probably means you're not setting up the Inspector correctly. First, you need to add your ChipScript
to the gameObject. You should capitalize it, and just call it Chip
. Some people do prefer adding Script
on the end, but imo it's redundant. Then, you should attach it to your GameObject. Here is how I've done so, in my project: https://rainChu.com/s/tG.png Once your $$anonymous$$onoBehaviour is attached as a Component on your GameObject, modifying the transform
property on it will work:
public void $$anonymous$$oving(float x, float z)
{
transform.position = new Vector3( x, 0f, z);
Debug.Log(x + " " + z);
}
In general, you only need to assign a GameObject in the inspector when you need to reference a different object. You can always find the GameObject that your chipScript
is attached to with the gameObject
property. Try this, and let me know if it helps!
I´ve checked everything and, as I instantiate a clone of the prefab, it seems I have to reference the clone ins$$anonymous$$d of the prefab(i dont really know why). I mean, when in run time i change the reference of the chip to the chip(clone), it actually works.
Anyways by reading your answers I´ve solved my issue, thanks!
Your answer
Follow this Question
Related Questions
How do you save the position of clone prefabs in a text file? 1 Answer
how do I save a script when access is denied? 0 Answers
LoadLevel when colliding an object and clicking on it ? 1 Answer
Months after I left my Bootcamp demo alone, I can't see anything anymore.. 1 Answer
Trying to get my pong AI to work 0 Answers