• 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 /
avatar image
0
Question by AnonymousCheese · Mar 09, 2021 at 08:46 AM · scripting problemtransform.position

Transform.Position not working

Looking to make a specific gameobject be set to active and move to a specific position for an OnClick function. Everything works except the Transform.Position, as soon as I try to implement it, the Gameobject, which ordinarily is set to active OnClick, actually just completely disappears.

For context, the gameobject is ordinarily positioned at -278, 742, 0. And needs to move to -253, 483, 0 when the script runs it's if statement and finds the bool to be true. It's a 2D project, so I don't actually need to use Vector3, but in all the examples I've found, it just seems to be the staple everyone is using regardless. Changing the Z value doesn't change anything as far as visibility is concerned.

I'm not really sure what I'm doing wrong here. I'm sure that the solution is going to be really stupidly simple, or completely out of my league, haha, so any help would be appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI;
 
 public class isActive : MonoBehaviour
 {
     
     bool Home = true;
     public GameObject YouAreHere;
     public Button DeactivatedButton;
     public GameObject ToMove;
 
     
     // Start is called before the first frame update
     void Start()
     {
         if(Home == true)
         {
             Debug.Log("You're at home already!");
             YouAreHere.SetActive(true);
             DeactivatedButton.interactable = false;
             ToMove.transform.position = new Vector3(-253f, 483f, 0f);
         }
         else
         {
             Debug.Log("Well, look at that, you aren't at home!");
             YouAreHere.SetActive(false);
             DeactivatedButton.interactable = true;
         }
     }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AnonymousCheese · Mar 09, 2021 at 05:56 PM

Answering my own question here. transform.localPostion is what solved it. From the research I did, I didn't think transform.localPosition would work because what I was trying to move wasn't a child to the parent I was moving it to.

Thank you for the couple of you that tried to help! Next time I think I'll just try all the options that I assume won't work before asking a question here haha.

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 GeroNL · Mar 09, 2021 at 09:18 AM

hello, i'll ask and explain :


Is there no update position for ToMove except here? cause it just on start then maybe you update in some where else.


And are you positioning by local position of Gameobject or not? the better do to change position like that maybe just use Gameobject pos to gameobject pos (i mean you make anchor with other object that do not have meshes or something like that), cause when you drop that hard code value ( new Vector3(-253f, 483f, 0f);), you still focus on Gameobject wich the gameobject is under of other Gameobject (it's have a parent), so it will not showing real position/world position, it's showing local position.


Hope it help.

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 AnonymousCheese · Mar 09, 2021 at 04:50 PM 0
Share

I've tried moving this all to the update function, that doesn't work either. The problem is I need the entire if-statement to be triggered at the same time, because the ToMove gameobject moves to different positions based on which bool is set to true, which in this case is "Home". Later on there will be triggers/booleans that are set to true/false based on where the character is located, so rather than having 6 different ToMovegameobjects that all move to their correlated places, I only wanted one game object ToMoveto specific places based on which bool == true

I'm working in a test environment in a test scene, which I use for implementing new features before coding them into the rest of what I have.

The gameobjects are text. There is nothing above or below them. I can turn off every other layer so I can only see the two text options and the one I'm trying to move still disappears.

Is what you're telling to me try to use transform.localPosition to see if I get different results, rather than just transform.position? Sorry your answer was a little confusing.

avatar image GeroNL AnonymousCheese · Mar 09, 2021 at 05:47 PM 0
Share

no, i just ask to make sure it's not have update position that make the position changed, if just on there then it's fine, if any set position for that in update, that make changing the position.


sorry if make you confuse, what i mean is transfom it by object, make other object to anchor your next position, something like this :

 //make object just for anchor
 Gameobject AnchorNextPos; // 
 .....
 // then ToMove set the pos by it
 ToMove.transform.position = AnchorNextPos.transform.position;

avatar image GeroNL GeroNL · Mar 09, 2021 at 05:53 PM 0
Share

it's the button generated? or just setactive true and false?


if generated, i think you must find the reference of the object first, maybe cause your object reference are null.

avatar image
0

Answer by patrickjong123 · Mar 09, 2021 at 05:08 PM

First of all, since you work in a 2D space you can use a Vector2D. You can move it by changing positition over time by using a coroutine. https://docs.unity3d.com/Manual/Coroutines.html

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 AnonymousCheese · Mar 09, 2021 at 05:50 PM 0
Share

Vector2/Vector3, as I mentioned, doesn't matter. I can use either, and it's the method I used because it's the most common example given when using vectors, the difference is one allows me to see 3D changes, and if the z axis is set to 0 anyway, it doesn't matter.

I don't want to move it gradually. I want the change to be instant. Which is why I wanted to use transform.position. When the bool == true, the ToMove object is set to active, and moved instantly to the specified position. If a coroutine can achieve this, that's great, but my understanding is that a coroutine is used most specifically when actions need to take place over time, not instantly, which is why IEnumerators are needed.

Also, linking me to the documentation on how to implement a coroutine doesn't help, sorry. Especially when the given example is for fading an objects alpha, rather than moving a gameobject. I'm not looking for you to solve my code, but maybe providing a little more insight on why it isn't working would be more valuable than shoving possibly irrelevant documentation at me and telling me to change it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Instantiate object at specific distance from transform position? 1 Answer

hey, is it possible to use a teleport transform script that will teleport you to a certain location on a different scene? 2 Answers

Moving parent from child' script doesn't work 0 Answers

How can i change position of my character 0 Answers

Is it possible to intercept variable alteration/function calls on an object (From within the object)? 0 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