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 xotsuj · Jul 08, 2018 at 02:41 PM · instantiatetriggervector3transform.position

instantiate keeps repeating itself

I'm new to unity and c# but I have been trying to recreate the game color switch. I followed the tutorial online about how to make the wheels, their rotations, collisions and the basic player mechanics. However, the tutorial did not show how to make the game infinite therefore for the past 9 hours I have been trying to accomplish that. I keep running into the same problem where the rings (only the larger one inside the smaller one) spawn multiple times when the green dot which gives the point and spawns in a new circle is consumed. I know there is probably a stupid error and you think I'm just trying to waist peoples time however I am really stuck and any help is appreciated. Thank you.`

 using UnityEngine;

 using UnityEngine.SceneManagement;

 public class Player : MonoBehaviour {

 public float jumpForce = 7f;

 public Rigidbody2D rb;

 public SpriteRenderer sr;

 public string currentColor;

 public GameObject Green;

 public GameObject[] obj;

 public float SpawnAmount = 1f;

 public Color ColorBlue;
 public Color ColorPurple;
 public Color ColorYellow;
 public Color ColorPink;
 public int Score = 0;
 public int Height = 0;
 Vector3 CurrentSpawnPoint;
 private void Start()
 {
     SetRandomColor();
 }



 void Update() {

     if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
     {
         rb.velocity = Vector2.up * jumpForce;
     }

 }
 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.tag == "Change")
     {
         SetRandomColor();
         Destroy(col.gameObject);
         return;
     }

     else if (col.tag == "Point")
     {
         Debug.Log("Point gained");
         Destroy(col.gameObject);
         Score += 1;
         Height += 7;
         CurrentSpawnPoint = col.transform.position;
         CurrentSpawnPoint.y += 7;
         Instantiate(Green, CurrentSpawnPoint, Quaternion.identity);
         Instantiate(obj[Random.Range(0, obj.GetLength(0))], CurrentSpawnPoint, Quaternion.identity);
         
         Debug.Log(Height);
         Debug.Log(CurrentSpawnPoint);
         return;
     }

     else if (col.tag != currentColor)
     {
         Debug.Log("GAME OVER");
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 }
 void SetRandomColor()
 {
     int index = Random.Range(0, 4);

     switch (index)
     {
         case 0:
             currentColor = "Blue";
             sr.color = ColorBlue;
             break;
         case 1:
             currentColor = "Yellow";
             sr.color = ColorYellow;
             break;
         case 2:
             currentColor = "Purple";
             sr.color = ColorPurple;
             break;
         case 3:
             currentColor = "Pink";
             sr.color = ColorPink;
             break;
     }


 }

}`

Comment
Add comment · Show 9
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 Guy_Yome · Jul 09, 2018 at 12:47 AM 0
Share

Well, do you have any script attached to the "Green" object or the other one that might do somethings in the Start function?

avatar image Guy_Yome · Jul 09, 2018 at 12:49 AM 0
Share

Also, is there any more object that have the tag "Points" that should not have it, ie the floor?

avatar image NoDumbQuestion · Jul 09, 2018 at 01:57 AM 0
Share

Pretty sure what happen is when you instaniate another GameObject. Enter2D called again for the new gameobject.

This part:

     Instantiate(Green, CurrentSpawnPoint, Quaternion.identity);
              Instantiate(obj[Random.Range(0, obj.GetLength(0))], CurrentSpawnPoint, Quaternion.identity);

So what happen is when NewGameObject instaniated. Your Wheel call trigger too with the new gameobject. => Call Trigger again => make a new gameobject again -> infinite loop

avatar image Guy_Yome NoDumbQuestion · Jul 09, 2018 at 02:21 AM 0
Share

Probably, but the destroy is called before the gameobjects are instantiated.

avatar image NoDumbQuestion Guy_Yome · Jul 09, 2018 at 02:23 AM 0
Share

Destroy called after update() done. He should have call Destroy immediately

Show more comments

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

140 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

Related Questions

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers

How to make another ground spawn beneath itself 1 Answer

i got two rotating objects that hit each other so i want to instantiate sparks at colliding point. (oncollision) and (ontrigger) 1 Answer

Can’t keep the position 0 Answers

objects not spawn where they should 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