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 unity_5ZilTRxtsFq7bg · May 22, 2020 at 11:52 PM · instantiatetransform.positioninstantiate prefabmovetowards

Moving a GameObject that was instantiated,Problem moving an instantiated Object.

So, I've been trying solutions for a dozen hours to no avail. What I want to achieve is to just move my instantiated GameObject to a fixed location when certain conditions are met. I have a big script that calls this lesser one that corresponds to the object. Script is fairly big, so I'll just copy the important details.

 public class EnemyController : MonoBehaviour
 {
     
     Vector3 Position;
     Vector3 HomePosition;
 
     void Start()
     {
         
         Position = transform.position;
         HomePosition = transform.position;
 
     }
 
     void Update()
     {
         EnemyMovement();
     }
 
    
     public void ChangePosition(Vector3 NewPos) //This is called from the other script to change the position
     {
         Position = NewPos;
         Debug.Log("New position should be " + Position);
         Debug.Log("Home position is " + HomePosition);
         Debug.Log("The distance is " + Vector3.Distance(Position, HomePosition));
 
     }
 
     public void EnemyMovement()
     {
         float step = Time.deltaTime * 1.0f;
 
         if (Vector3.Distance(Position, HomePosition) > 0)
         {
             
                 transform.position = Vector3.MoveTowards(transform.position, Position, step);
                 Debug.Log("Should be moving here");
                
         }
 
     }
 }

The code runs fine and does its thing, but when the time to move arrives, it doesn't do it. It feels as if the Update() (Which btw works fine at the start of the scene) just stops updating, but when I use a Debug.Log on the Update is never stops. I don't understand what I'm doing wrong.

On the Big script I instantiate my object like so:

 MainEnemy = (GameObject)Resources.Load("Enemy/" + EnemyStatic.EnemyCode, typeof(GameObject));
         FirstStation.SetActive(true);
 
         Instantiate(MainEnemy, EnemyStation1);

This creates a copy of a new GameObject, whith its own code and everything. Note that when in the object script I put the Vector3.MoveTowards on the Update Method it works instantly, it is when I change the Position that it doesn't work. My debugs on the ChangePosition() give me the expected anwers, but still, not working.

I'm fairly new to unity, so answers for a 10yo are appreciated!

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

0 Replies

· Add your reply
  • Sort: 

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

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

Invisible Prefab spawning at location of Player before being instantiated 1 Answer

Can't set transform.position AND constantforce.force? 1 Answer

Get a Insantiate particle system follow a GameObject 1 Answer

Move Gameobject towards/away from position based on the proximity of 2 other objects 0 Answers

Pickup instantiates from a projectile, but not where it is supposed to instantiate. 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