Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Basel33 · May 22, 2016 at 08:01 PM · positionchangeif statement

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;
     }

 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · May 23, 2016 at 06:27 PM 1
Share
  • 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Basel33 · May 23, 2016 at 04:08 PM 0
Share

So what you think I must do?

avatar image Le-Pampelmuse Basel33 · May 23, 2016 at 06:18 PM 1
Share

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! ;)

avatar image Basel33 Le-Pampelmuse · May 23, 2016 at 06:45 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Basel33 · May 23, 2016 at 04:06 PM 0
Share

I think this is not working because its still not write ("out") in the console.

avatar image Le-Pampelmuse Basel33 · May 23, 2016 at 06:07 PM 1
Share

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.

avatar image Basel33 Le-Pampelmuse · May 23, 2016 at 06:42 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges