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 danz1ka19 · Mar 02, 2017 at 06:01 PM · gameobjectsynchronizationparent-child

How to avoid Synchronous Script Behaviour

Hey guys,

I am having problem in building a clone for Space Invaders 1978. My game has 11 columns with 5 rows of aliens. The column has a script that checks for the 1st alien in the row and allows it to fire. The problem is that each column is behaving synchronously with other column scripts Example: if column1 fires a bullet, Column2-11 can't fire until Column1's bullet is finished, I want a asynchronous behave such that no columns are dependent on one another.

The ColumnScript is as:

 public float min = 0;
 public float max = 10;

 void Start () {
     float rand = Random.Range (min, max);
     Invoke ("SelectForFire", rand);
 }

 void SelectForFire () {
     if (transform.childCount > 0) {
         transform.GetChild (0).gameObject.GetComponent<EnemyScript> ().Invoke ("Fire", 0f);

         float rand = Random.Range (min, max/2);
         Invoke ("Start", rand);
     }
 }

and the EnemyScript as:

 public Rigidbody2D bullet;

 void Fire () {
     float x = transform.position.x;
     float y = transform.position.y - 0.4f;
     Instantiate (bullet, new Vector2 (x, y), Quaternion.identity);
 }

How can I fix this?

Comment
Add comment · Show 3
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 brazmogu · Mar 03, 2017 at 07:28 PM 0
Share

Just to be clear here about the object tree you have here (because I have a suspicion, but we have to confirm it) what you have in your scene is something like:

Column A

Alien 1

Alien 2

Alien 3

Alien 4

Alien 5

Column B

Alien 1

Alien 2

Alien 3

Alien 4

Alien 5

Column C

Alien 1

Alien 2

...

Column D ...

Where each Column is a transform with the first code assigned as a behavior, correct?

avatar image danz1ka19 brazmogu · Mar 04, 2017 at 03:16 PM 0
Share

@brazmogu It means that void OnBecameInvisible is called when the bullet leaves the camera and destroys it via Destroy(gameObject)

avatar image hexagonius · Mar 04, 2017 at 12:15 AM 0
Share

I don't see the dependency you're talking about. when every column has it's ColumnScript, how should they be able to prevent one another to do something? I can't see that happening

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brazmogu · Mar 04, 2017 at 03:22 PM

So, assuming everything else is in order, the only thing I'm not very familiar with here is the use of Invoke. The docs state it's as simple as a delayed "SendMessage", but the impression I'm getting is that it's less asynchronous that it seems.

Point in case, you could do this with co-routines instead. So the code for your ColumnScript becomes

  public float min = 0;
  public float max = 10;
  void Start () {
      float rand = Random.Range (min, max);
      StartCoroutine (SelectForFire (rand) );
  }
  IEnumerator SelectForFire (float delay) {
      yield return new WaitForSeconds(delay);
      if (transform.childCount > 0) {
          transform.GetChild (0).gameObject.GetComponent<EnemyScript> ().SendMessage ("Fire");
          float rand = Random.Range (min, max/2);
          yield return SelectForFire (rand);
      }
  }

and EnemyScript would be like:

  public Rigidbody2D bullet;
  void Fire () {
      float x = transform.position.x;
      float y = transform.position.y - 0.4f;
      Instantiate (bullet, new Vector2 (x, y), Quaternion.identity);
  }

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

112 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

Related Questions

how can i get the gameobject socket by XRsocket interactor 0 Answers

Instatiate a gameobject/prefab on the parent's current location if the child is destroyed. 1 Answer

Cant get setParent to actually set the parent for child. 0 Answers

How to set parent to components object? 1 Answer

Sprite changes the child local position,Sprite pivot change Vector3.zero postion of child gameObject 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