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 /
This question was closed Feb 03, 2016 at 07:26 AM by Hydropulse17 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Hydropulse17 · Feb 03, 2016 at 06:06 AM · vector3variablestransform.positionvaluesboundary

Script works with hard coded values but not variables

I have a world wrap script that works flawlessly, but if I try to replace the values with variables of EXACTLY the same value, everything falls apart. I even tried replacing one value at a time and checking it to be extra careful.

Here's the script. The variables at the top represent the values in the function.

     public int rightWall = 100;
     public int leftWall = -110;
     public int topWall = 190;
     public int bottomWall = 50;
 
    void Update ()
     {
         if (transform.position.x <= -110)
         {
             transform.position = new Vector3(100, transform.position.y, 0);
         }
 
         if (transform.position.x >= 101)//+1
         {
             transform.position = new Vector3(-109, transform.position.y, 0);//+1
         }
 
         if (transform.position.y >= 190)
         {
             transform.position = new Vector3(transform.position.x, 50, 0);
         }
 
         if (transform.position.y <= 49)//-1
         {
             transform.position = new Vector3(transform.position.x, 189, 0);//-1
         }
     }

The comments represent what I would add or subtract from the variables so that the objects are within the boarders when their position is changed to the other side.

Comment
Add comment · Show 5
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 saschandroid · Feb 03, 2016 at 06:52 AM 0
Share

Do you get any errors when replacing the values with variables? Vector3 takes float values as input, but your variables are ints.

avatar image Hydropulse17 saschandroid · Feb 03, 2016 at 06:57 AM 0
Share

I do not get any errors, but I tried using floats ins$$anonymous$$d anyways, it still doesn't work.

avatar image saschandroid Hydropulse17 · Feb 03, 2016 at 07:03 AM 1
Share

The variables are public. Are you sure you haven't changed the values outside this script? Look at the values in the inspector or/and Debug.Log them.

Show more comments
avatar image SterlingSoftworks · Feb 03, 2016 at 07:08 AM 0
Share

What about setting up a temp Vector3 that has the variables.

Something like

Vector3 rightWallPos = new Vector3(rightWall, transform.position.y, 0);

//and then set the transform

transform.position = rightWallPos;

I don't know if that will work, and I have no idea why it won't work with the variables :O Curious..

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Hydropulse17 · Feb 03, 2016 at 07:20 AM

@saschandroid solved my issue in the comments. My variables were public, and were different on the object prefabs from the script. I didn't change them, I initiated them in the script and never touched the inspector, but that was in fact the problem. Resetting the monobehavior component on the prefab fixed my issue.

Comment
Add comment · 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
0

Answer by MrMatthias · Feb 03, 2016 at 06:45 AM

I'm not sure what you are trying to do, but you set the position to fixed positions, there won't be any movement. To add or subtract values use something like this:

 transform.position += new Vector3 (1, 0, 0); // move by 1 in x direction

you might also want to make it dependent on Time.deltaTime otherwise the speed will depend on the speed of the running pc.

 transform.position += new Vector3 (xSpeed * Time.deltaTime, 0, 0);



Comment
Add comment · Show 1 · 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 Hydropulse17 · Feb 03, 2016 at 07:00 AM 0
Share

It needs to teleport to the other side, a whole new position is being assigned. $$anonymous$$y script is not flawed, like I've said, it works fine without variables in place of the hard values.

Thank you for your answer anyways, because that second tip will actually help me with another problem I've been having.

Follow this Question

Answers Answers and Comments

38 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.position not adding correctly with a Vector3 1 Answer

Object doesn't move 2 Answers

How to use a Vector3.MoveTowards and the resulting position as an input for a normalised BlendTree 0 Answers

Get an Object to move to a Position for a Fixed time 2 Answers

How to teleport gameobject (instantly change transform.position) 3 Answers


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