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 Smusi_123 · Aug 29, 2019 at 02:10 PM · movementinstantiatebugspawningspace invaders

Strange bug in a Space Invaders type game

Hi,i am new to unity and i am trying to create a game like Space Invaders, but i have a bug in the movement script of the invaders. in the Spawn script that is attached to an empty gameobject(scene controller), i create 40 invaders by instantiate the movement serializedfield and i fix their position, then i memorize all the invaders in a list; Once done with that i pass this gameobject in the movement script (that is attached to each of invaders) as serializedfield so i can retieve the list with all the invaders;here first i create a function to controll the object with max pos and with less pos, so i find the invader of the right border and of the left border; in the update method i move the invader since the invader of right border has an position <18, when is>18 i change the speed to negative, and the same for the other side. the problem is that when i run the game only the row with the invader of right border and left border acts in a strange way, when the right border arrives at 18 and changes his speed to negative , all the invaders of the row change their position a little to left and same happens for the other side; alt text

as you can see only the row with the two invaders that i use for changing velocity after a few time shifts without a reason,this movement script;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class InvaderMovement : MonoBehaviour
 {
     [SerializeField]private Spawner a;
 
     public float MinX = -18;
     public float MaxX = 18;
     public float speed=90;
     private int b = 1;
     private int indexMAX;
     private int indexMIN;
     private bool Dir = false;
 
     void Start()
     {
         speed = -3;
         confrontall();
         Debug.Log(indexMAX);
         Debug.Log(indexMIN);
         Debug.Log(a.AllInvaders[indexMAX].transform.position.x);
 
     }
 
     void Update()
     {
         transform.Translate(new Vector3(speed, 0, 0));
 
         if (a.AllInvaders[indexMAX].transform.position.x >= 18)
         {
             speed = -speed;
             Debug.Log(speed);
             Debug.Log("cio");
             
             Dir = true;
         }
 
         if (a.AllInvaders[indexMIN].transform.position.x <= -18 )
         {
             Debug.Log(speed);
             speed = Mathf.Abs(speed);
             Dir = false;
         }
     }
 
     public void confrontall()
     {
             float max = -1000;
     float min = 1000;
 
         for(int j=0; j<a.AllInvaders.Count;j++)
         {
             if (a.AllInvaders[j] != null)
             {
                 if (a.AllInvaders[j].transform.position.x > max)
                     max = a.AllInvaders[j].transform.position.x;
                 if (a.AllInvaders[j].transform.position.x < min)
                     min = a.AllInvaders[j].transform.position.x;
             }
 
         }
         for (int i=0; i<a.AllInvaders.Count;i++)
         {
             if (a.AllInvaders[i] != null)
             {
                 if (max == a.AllInvaders[i].transform.position.x)
                     indexMAX = i;
                 if (min == a.AllInvaders[i].transform.position.x)
                     indexMIN = i;
             }
 
         }
     }
 
 }
 

this is the Spawn script;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour
 {
     [SerializeField] private InvaderMovement invader;
     private InvaderMovement obj;
     internal List <InvaderMovement> AllInvaders = new List<InvaderMovement>();
     private float offsetX = 3;
     private float offsetY = 2;
     private int k = 0;
 
     void Awake()
     {
         int max = 5;
         Vector3 OriginalPos = invader.transform.position;
         for (int i = 0; i < 4; i++)
         {
             max = 10;
              OriginalPos = invader.transform.position;
                 OriginalPos.x = OriginalPos.x - offsetX;
             for (int j = 0; j < max; j++)
             {
                 AllInvaders.Add(Instantiate(invader) as InvaderMovement);
                 AllInvaders[k].transform.position = new Vector3(OriginalPos.x + offsetX, OriginalPos.y -(i * offsetY), OriginalPos.z);
                 OriginalPos.x = AllInvaders[k].transform.position.x;
                 k++;
             }
         }
         Destroy(invader.gameObject);
     }
     
 }


here i also put the inspector of the two object. I hope someone can help me

alt text

capture1.png (174.5 kB)
untitleddd.png (70.5 kB)
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

266 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

problems with instantiating a moving prefab. 0 Answers

Having way too much difficulty synching my objects (Unity Networking) 0 Answers

IsTouching() not working right after Colliders2D movement/instanciation 0 Answers

How to make multiple objects spawn at random order and move ? 1 Answer

Problem instantiating objects one after another with mouse click 1 Answer


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