Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by WNTRSWE · May 06, 2021 at 03:14 PM · gameobjectspawningchildrenpositioningspawning problems

Instantiated children wont update position

So I decided to make a game for a school project just for fun, it's a typical fly around dodge asteroids 2d game. I have run into one problem though. I have created an empty game object with a square that shows the "spawnable" area for the asteroids. But when I move and the camera/the empty game object updates its position. The spawn area for the asteroids won't, the square will follow the change in position but the asteroids won't.


I have pasted my script that is basically copied from a youtube video and I will also link a youtube video that I recorded to show more in-depth what I mean


Thanks in advance


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class deployDodgebles : MonoBehaviour
 {
     public GameObject circlePrefab;
     public Transform positionSpawner;
     public GameObject circles;
     public float respawnTime = 1;
 
     private void Start()
     {
         StartCoroutine("respawnEnemie");
     }
 
     private void spawnEnemie()
     {
         circles = Instantiate(circlePrefab) as GameObject;
         circles.transform.parent = positionSpawner.transform;
         circles.transform.position = new Vector2(Random.Range(-13.49813f, 13.49813f), 7.347601f);
     }
 
 
     IEnumerator respawnEnemie()
     {
         while (true)
         {
             yield return new WaitForSeconds(respawnTime);
             spawnEnemie();
         }
     }
 
     private void OnBecameInvisible()
     {
         Destroy(circles.gameObject);
     }
 }



https://www.youtube.com/watch?v=WxzoIdLjs-Y

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GSGregory · May 06, 2021 at 03:39 PM

That video helped quite a bit. I know its in the early stages but consider naming your circles what they are supposed to be.

Anyway your circles.transform.position = new Vector2(Random.Range() is the problem. Its static. It should be something like (-13f-circles.transform.parent.position, 13f+circles.transform.parent.position) so that the position is relative to the parent position.

Comment
Add comment · Show 4 · 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 WNTRSWE · May 06, 2021 at 03:48 PM 0
Share

Thanks for the quick response, although I'm not sure what you mean? what should go where in the script?

avatar image WNTRSWE · May 06, 2021 at 04:11 PM 0
Share

Not sure what you mean by "(-13f-circles.transform.parent.position, 13f+circles.transform.parent.position)" should I replace that instead of random.range?

avatar image GSGregory WNTRSWE · May 06, 2021 at 04:39 PM 0
Share

Sorry had to do something else before I could get back to you. This is what you want.

      private void spawnEnemie()
      {
          circles = Instantiate(circlePrefab) as GameObject;
          circles.transform.parent = positionSpawner.transform;
          Vector2 parentPosition = new Vector2(circles.transform.parent);
          circles.transform.position = new Vector2(Random.Range(-13.49813f+parentPosition.x, 13.49813f+parentPosition.x), 7.347601f);
      }
avatar image WNTRSWE GSGregory · May 06, 2021 at 05:17 PM 0
Share

Thanks a lot man, fixed it by using your code and adding the parentsposition on the y axis too so it's following it there too

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class deployDodgebles : MonoBehaviour
 {
     public GameObject circlePrefab;
     public Transform positionSpawner;
     public GameObject circles;
     public float respawnTime = 1;
 
     private void Start()
     {
         StartCoroutine("respawnEnemie");
     }
 
     private void spawnEnemie()
     {
         circles = Instantiate(circlePrefab) as GameObject;
         circles.transform.parent = positionSpawner.transform;
         Transform parentPosition = circles.transform.parent;
         circles.transform.position = new Vector2(Random.Range(-13.49813f + parentPosition.transform.position.x, 13.49813f + parentPosition.transform.position.x), 7.347601f + parentPosition.transform.position.y);
     }
 
 
     IEnumerator respawnEnemie()
     {
         while (true)
         {
             yield return new WaitForSeconds(respawnTime);
             spawnEnemie();
         }
     }
 
     private void OnBecameInvisible()
     {
         Destroy(circles.gameObject);
     }
 }
avatar image
0

Answer by vargata · May 06, 2021 at 04:53 PM

use Instantiate(Object original, Vector2 position, Quaternion rotation, Transform parent); where position and rotation will be relative to parent...

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

180 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

Related Questions

using DrawMesh to draw all sub meshes and materials for a game object and children 1 Answer

How Should I Get a List of Child Objects 2 Answers

How to Access Components in Children of GameObject? 1 Answer

Why is the wrong child activated, although I have the right index? 1 Answer

if i have instantiated gameobjects under a parent object and then after instantiating i need to despawn some of these children randomly how can I achieve that . 2 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