• 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
1
Question by MercylessK · Aug 24, 2019 at 05:22 PM · transformprefabscript.issueinstantiate prefab

Prefab with same scripts do the same thing

In my game 2D I instantiate 2 prefabs that contain the same simple script that basically moves the object back and forward within the x axis, using his current x value. When the objects get instantiated, they get different x coordinates at the start, so let say prefab1 starts with position 100 and prefab2 starts with position 400 (The instantiation is done using a script in the GM). The issue is that as soon as the scene starts both prefabs begins its movement in the same position as if they ignored the instantiated starting x coordinate.

See the code for the instantiation:

 public GameObject wallAutoPrefab;

     void Fase23(){
         // Constantes - Posições
         alvoPos1 = new Vector2(0,384);
         playerPos1 = new Vector2(0,-512);
         wallPos1 = new Vector2(-192,192);
         wallPos2 = new Vector2(192,-192);
         // Constantes - Escalas
         wallScale1 = new Vector3(32,1,1);
         wallScale2 = new Vector3(32,1,1);
         // Instancia Objetos
         GameObjectUtil.Instantiate(alvoPrefab, alvoPos1, consRot, consScale);
         GameObjectUtil.Instantiate(playerPrefab, playerPos1, consRot, consScale);
         GameObjectUtil.Instantiate(wallAutoPrefab,wallPos1,consRot,wallScale1);
         GameObjectUtil.Instantiate(wallAutoPrefab,wallPos2,consRot,wallScale2);
     }


See the code for the automovement that is attached in the wallAutoPrefab prefab:

 public class AutoMove : MonoBehaviour
 {
     private float min;
     private float max;
     private float t;
     private bool inverter = false;
 
     void Start(){
         if (transform.position.x > 0) {
             min = transform.position.x;
             max = min * -1;
         } else {
             max = transform.position.x;
             min = max * -1;
         }
     }
 
     void Update()
     {
         if (inverter == false) {
             transform.position = new Vector3 (Mathf.Lerp (min, max, t), transform.position.y, transform.position.z);
             t += 1.0f * Time.deltaTime;
             if (transform.position.x == max) {
                 inverter = true;
                 t = 0.0f;
             }
         } else if (inverter == true) {
             transform.position = new Vector3 (Mathf.Lerp (max, min, t), transform.position.y, transform.position.z);
             t += 1.0f * Time.deltaTime;
             if (transform.position.x == min) {
                 inverter = false;
                 t = 0.0f;
             }
         }
     }
 }

I tried everything, using foreach, using this code in the GM but I could not make it work. Please help me out here!! Thanks!

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.

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

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

Acces position of previously instantied prefab 0 Answers

how add child to multi gameobject 0 Answers

How to add Components to some individuals parts of a prefab 1 Answer

Destroying prefabs breaking scripting for other prefabs 0 Answers

Instantiate objects in for loop 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