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 Lo_Mein_Gang · Jan 20, 2019 at 05:03 PM · movementprefabobjects-movement

Position of all spawned prefabs being effected by MoveTowards (beginner question)

Just want to begin by saying that I am an extreme new comer to Unity and game programming in general. So this question is probably going to be extremely obvious, but it is giving me trouble.

So to try and explain my issue as best as possible. My game im working on is a really simple shoot-em up type game where I have prefabs(enemies) spawning from the right side moving left. A gameplay element I have is that the player can occasionally shoot some hacking shots that will give you control of the enemies and they are basically the usually upgrades you would have in a shoot-em up.

My issue comes up when I shoot the enemies with the hacking shot i want to use the MoveTowards to move them to a designated location (emptyobject) by the player. I have it working so that when i shoot a single enemy on screen it will move to place. However, if there are more than one one screen they all start to move to the designated location even if I have only shot one of the enemies.

I feel like there is some way to only manipulate the position of a single prefab at a time and not change all of them together.

Let me know if there is any other info you could use to help solve my issue.

Thanks! using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class enemy2 : MonoBehaviour
 {
     public int speed;
 
     private Rigidbody2D rb;
 
     public GameObject enemyBullet2;
 
     private int bulletDelay = 6;
 
 
     private static Transform openSpot;
 
 
     // Movement speed in units/sec.
     public float moveSpeed = 50;
 
     // Time when the movement started.
     private float startTime;
 
     // Total distance between the markers.
     private float journeyLength;
 
     private static bool beginMove = false;
 
     private 
 
 
     // Start is called before the first frame update
     void Start()
     {
         
         rb = GetComponent<Rigidbody2D>();
 
         rb.velocity = Vector2.left * speed;
 
         StartCoroutine(spawnBullet());
 
     }
 
     // Update is called once per frame
     IEnumerator spawnBullet()
     {
 
             yield return new WaitForSeconds(.5f);
 
 
             for (int i = 0; i < 4; i++)
             {
             while (beginMove == false)
             {
                 Instantiate(enemyBullet2, transform.position, Quaternion.identity);
                 yield return new WaitForSeconds(1);
             }
             }
         
 
     }
     private void Update()
     {
         if (beginMove == true)
         {
 
 
             rb.velocity = Vector2.left * 0;
             // Move our position a step closer to the target.
             gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, openSpot.position, 1);
 
             if (gameObject.transform.position == openSpot.position)
             {
                 beginMove = false;
             }
         }
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.tag == "despawner")
         {
             Destroy(gameObject);
         }
 
         if(col.tag == "Player" && beginMove == false)
         {
             Destroy(gameObject);
         }
 
         if (col.tag == "hacker")
         {
             if (Spaceship.currentSpot < 6) { 
             
             beginMove = true;
             openSpot = Spaceship.nextOpenSpot.transform;
            }
         }
     }
 
 
 
 }

,

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

1 Reply

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

Answer by tormentoarmagedoom · Jan 20, 2019 at 07:19 PM

Good day.

The probelm is at your definition of variables whe you make a variable static.

A static variable means there is only 1 variable of that in all the scene. Not one for each object, only 1 general for all. All objects will acess and read the same variable.

You want that every enemy have its own destination, so that can not be a static variable.

Bye!

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

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

240 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

Related Questions

how to instantiate a prefabs that move upward on update function and get the transform position of that moving prefab 0 Answers

FPS controller movement problems 0 Answers

Physics Question 0 Answers

Initialize Prefab with all it's scripts 2 Answers

My Prefab goes crazy and it moves faster when i click in another thing of the unity editor or the scene even if that didn't suppose to happen 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