How I can change my game object position.x in current position?
I have a column and I want to change it position.z to -100 when it arrived to position.x 7 my code is not working and her he is ... any help
using UnityEngine; using System.Collections;
public class Column : MonoBehaviour { public float coulmnSpeed = 0.1f; Vector2 tempPos; public Transform coultransform; bool arrived; void Start () {
}
void Update () {
tempPos = transform.position;
tempPos.x = tempPos.x + coulmnSpeed;
transform.position = tempPos;
if (coultransform.position.x == 7 && arrived == false) {
arrived = true;
Debug.Log ("out");
tempPos.x = tempPos.x - 100;
}
}
}
What have you tried?
Does arrived get set to false anywhere? If not, the whole thing will not do anything.
You didn't change the transform's position to the actual new position vector tempPos.
You need to provide more general information about your script and what problem you are having.
Posting your script and asking us to fix it is not how this forum works. Please consider this in the future.
Also, like I mentioned below, please edit your question to format the code properly.
http://answers.unity3d.com/questions/427769/gameobjecttransformpositionx-.html
Answer by tanoshimi · May 22, 2016 at 08:20 PM
You're moving transform.position to the right by columnSpeed each frame.
You're testing whether coultransform, a different variable, ever reaches .x == 7...
You must:
Format your code properly. Use the 101010 button on the highlighted code. Every part of the code. also the
using Untiy...
parts.Watch scripting tutorials. "How to change position x of a gameobject" is highly beginner coding. Unity Answers is not for problems with beginner topics that are tought in documentation or tutorials.
Your problems are realted to changing transform.position and using if statements.
Check out the tutorials or docs (you can find everything using google) about those topics and you are ready to solve the problem.
PS: I don't mean to sound rude, I only want to tell you how to avoid getting downvotes or wasting your own time by writing a question and waiting for an answer if you can just google the problem. Don't be afraid to google your questions. ;) :P
In fact, if you ask a new question and it is a question like "how to do this and this in Unity", and the answer is clearly in the docs or tutorials, your question will be closed/rejected because it would be useless for anyone.
Have a nice day. Don't hesitate to start learning! ;)
I really appreciate your reply. As you know it took from me a lot of time to learn coding on c#. I am starting build my new own beginner game. So, I need a lot of help in this. thanks again
Answer by Sachin_H · May 23, 2016 at 12:49 PM
You will have to assign the tempPos variable to your position.z after you change the value
void Update () {
tempPos = transform.position;
tempPos.x = tempPos.x + coulmnSpeed;
transform.position = tempPos;
if (coultransform.position.x == 7 && arrived == false) {
arrived = true;
Debug.Log ("out");
tempPos.x = tempPos.x - 100;
// Assigning the variable the position of the transform
transform.position = tempPos;
}
}
Hope this helps
I think this is not working because its still not write ("out") in the console.
That's because your condition depends on arrived == false
too. Of course it will not execute the code inside the condition if only the coultransform.position.x == 7
part is true.
$$anonymous$$ake sure arrived is false and it will work. Or remove the arrived check.
thank you i think now it works i removed bool arrived and i replace (==) in if- statement with (>) and it works good because the real problem was in if-statement
Your answer
Follow this Question
Related Questions
Change position parameters of object onMouseOver 0 Answers
Is there a way to allow the user to change the position of the game object in game? 0 Answers
I'm making a simple platformer and I want the position to reset when you fall off the screen 2 Answers
OntriggerEnter reset gameobject position. 0 Answers
Weird position change 1 Answer