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 /
avatar image
0
Question by _minirlz · Jan 10, 2017 at 04:21 PM · unity 5instantiateloopforloop

For loop and instantiate question

Howdy,

I have written myself a script that allows me to instantiate a potato plant object on an available tile (as in no other plant is on that tile) and when I right click that plant and it is "mature" potatos will be instantiated and the plant will be destroyed. Once the plant is on longer on the tile it allows me to plant another one there again.

This all seems to be working as intended but now rather than 1-2 potatos spawing I'm getting 6-10. I know the issue it the for loop its self but I need it in order to loop through the different ground tiles.

Here is the method that allows me to destroy a mature plant and spawn the potatos

private void DeystroyMaturePlant() { Vector3 mousePOS = Input.mousePosition;

         if (maturePotato == true)
         {
             mousePOS = Camera.main.ScreenToWorldPoint(mousePOS);
             if (((gameObject.transform.position.y < mousePOS.y) && (gameObject.transform.position.y > mousePOS.y - 0.8)) && ((gameObject.transform.position.x < mousePOS.x + 0.3) && (gameObject.transform.position.x > mousePOS.x - 0.3)))
             {

                 int range = UnityEngine.Random.RandomRange(1, 3);
                 for (int j = 0; j < range; j++)
                 {

                     GameObject newPotato = new GameObject();
                     newPotato.transform.position = gameObject.transform.position;
                     newPotato.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
                     newPotato.AddComponent<SpriteRenderer>().sprite = itemDataBase.FindItem("Potato").sprite;
                     newPotato.AddComponent<Rigidbody2D>();
                     newPotato.AddComponent<PolygonCollider2D>();
                     newPotato.GetComponent<Rigidbody2D>().isKinematic = true;
                     newPotato.AddComponent<Magnetism>();
                     newPotato.GetComponent<Magnetism>().strength = 2;
                     newPotato.GetComponent<Magnetism>().range = 11.1f;

                     newPotato.layer = 10;
                     newPotato.name = itemDataBase.FindItem("Potato").name;
                     

                 }
                 
                 Object.Destroy(this.gameObject);
                 
             } 
     }
 }


I also have a for loop on that one that randomizes how many will drop. Getting rid of that does reduce the amount that are dropped but not by much. But it also doesn't randomize the number of drops which I would like.

Here is the method with the for loop

 private void Update()
 {
     GameObject[] ground = GameObject.FindGameObjectsWithTag("Ground");
     till = new Till[ground.Length];

     for (int i = 0; i < ground.Length; i++)
     {
         till[i] = ground[i].GetComponent<Till>();
         if (Input.GetMouseButtonDown(1))
         {
             DeystroyMaturePlant();
             till[i].isPlantable = true;
         }
     }


    
 }

 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is there a difference between script and in-game sequence of a script loop? 1 Answer

Animation from Blender 0 Answers

Using ForEach Loop to change variables on prefabs 1 Answer

How do I instantiate multiple game objects using Photon unity network? 0 Answers

Instantiate a button at mouse click position, 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