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 mora133 · Jul 24, 2020 at 01:10 AM · spawning

Hello everyone A lil stuck in here when I´m trying to spawn an object only ONCE when two objects collide. here´s the code. Thanks in advance.

 public GameObject Maze2;
 public Transform Spawnpoint;


 public void Update()
 {

     if (GameObject.FindGameObjectWithTag("StarLevel2") == null)
     {

         Destroy(gameObject, .2f);
         Instantiate(Maze2, Spawnpoint.position, Spawnpoint.rotation);

     }
 }
Comment
Add comment · Show 2
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 Miaw666 · Jul 24, 2020 at 02:20 AM 0
Share

You instantiate more $$anonymous$$aze2 than one because you're saying spawn $$anonymous$$aze2 as long as StarLevel2 is null. Pretty much it's because it's in Update. I don't really have any good solution but thought I could at least tell you what the problem is. You can Destroy(gameObject); without any delay so that the code won't last long to spawn any more $$anonymous$$aze2 than one. This is all I have ;(

avatar image Miaw666 · Jul 24, 2020 at 02:22 AM 0
Share

Why don't you use OnTriggerEnter instead? Idk what you're trying to do exactly but I think OnTriggerEnter would work much better for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tocu-adrian · Jul 24, 2020 at 06:44 AM

Hi,

It is not clear why are you using the Update method, if you said that you want to spawn an object when two objects collide, then use OnTriggerEnter or OnCollisionEnter.

But if you want to keep your code, why are you not spawn the object first, and then destroy your gameobject?

And if you want to keep your code like this, i suggest you to use a bool variable. I don't know if this is the most recommended method but i think it works.

  public GameObject Maze2;
  public Transform Spawnpoint;
  private bool _isNull = true;
 
  public void Update()
  {
      if (GameObject.FindGameObjectWithTag("StarLevel2") == null)
      {
          Destroy(gameObject, .2f);
          if(_isNull)
          {
          Instantiate(Maze2, Spawnpoint.position, Spawnpoint.rotation);
           _isNull = false;
          }
      }
  }
Comment
Add comment · Show 2 · 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 mora133 · Jul 24, 2020 at 01:38 PM 0
Share

Thanks a lot, it worked like a bliss!! I really apreciate it.

avatar image tocu-adrian mora133 · Jan 21, 2021 at 11:54 AM 0
Share

You're welcome ! :)

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

133 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

Related Questions

How to only destroy the object the i am in. 3 Answers

Space Shooter: How can I spawn the asteroids more than once? 3 Answers

random spawn for multible objects 1 Answer

Can you trigger/run code from a unspawned objcts, that isnt in the scene yet? 1 Answer

Spawning + Following Player Script + Inspector 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