• 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
Question by $$anonymous$$ · Dec 12, 2017 at 11:51 PM · arraygenerationsynchronizationdifferent objects

Generator script not working due to lack of differentiation?

Hello, I have a generator script that basically generates the bulk of the objects in my game, but It is not differentiating between generating 100 stone or 100 grass, it simply generates about 50 of both. Thus far I have been unsuccessful of finding out how to deal with t$$anonymous$$s due to me not being good at using multiple arrays in cooperation with each other. Thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EcoGeneraterScript : MonoBehaviour {
 
     public GameObject Map;
     public GameObject[] GeneratedObjects;
 
     public int[] NumberofObjectsToGenerate;
 
     private float Cord_X = 0;
     private float Cord_Z = 0;
 
     private bool CanGenerate = true;
 
     void Update () {
         for ( int i = 0; i < NumberofObjectsToGenerate.Length; i++) {
             foreach (GameObject GeneratedGameObject in GeneratedObjects) {
                 if (NumberofObjectsToGenerate[i] > 0 && CanGenerate == true) {
                     Instantiate(GeneratedGameObject, new Vector3(Cord_X, 0.5f, Cord_Z),Quaternion.identity);
                     NumberofObjectsToGenerate[i] -= 1;
                     Cord_X += 1;
                         if (Cord_X >= 100) {
                             Cord_X = 0;
                             Cord_Z += 1;
                             if (Cord_Z >= 100) {
                                 CanGenerate = false;
                         }
                     }
                 }
             }
         }
     }
 }

 
Comment

People who like this

0 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 nt314p · Dec 13, 2017 at 01:06 AM 0
Share

Make a variable that is randomly generated from 0-100. Call it x or something.

One array will have a size of x and the other will have a size of 100-x

avatar image $$anonymous$$ nt314p · Dec 13, 2017 at 01:30 AM 0
Share

I think what you are referring to is the coordinations, these don't have anything to do with the number of objects that still need to be generated.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by MacDx · Dec 13, 2017 at 03:08 PM

The logic of your foor loops is not correct. There is no loop in your code that iterates the number of times indicated by each entry in your NumberofObjectsToGenerate array. What you're doing right now is instantiating one of every object in your GeneratedObjects array for every entry in your NOTG (yeah that's what I'll call NumberofObjectsToGenerate, from now on). So if your NOTG array has 7 entries then your code will spawn 7 of each (unless of course the condition of CanGenerate is false).

 //Here's an assumption I'm making about your arrays since you didn't described their contents
 //    Array Position    Array Name    Entry       Array Name        Entry
 //              0           NOTG       50        GeneratedObjects   Obj1
 //              1           NOTG       100      GeneratedObjects   Obj2
 //So I'm assuming that what you want to do is link the arrays by their position, meaning that
 //you want to spawn 50 of Obj1 and 100 of Obj2 
 //since the position in their respective array is the same
 //So to do that I'd do somet$$anonymous$$ng like t$$anonymous$$s:
 void Update(){
     for(int i = 0; i<NOTG.Length ; i++){
         //Copy the number since for some reason you want to decrease it w$$anonymous$$le iterating
         //Know that modifying an array or any collection w$$anonymous$$le iterating over it,
         //isn't always the best idea
         int numberOfInstances = NOTG[i];
         for(int j = 0; j<numberOfInstances; j++){
             Instantiate(GeneratedObjects[i], new Vector3(Cord_X, 0.5f, Cord_Z),Quaternion.identity);
             NOTG[i] --;
         }
     }
 }

And that's it. A couple of notes. First, never do t$$anonymous$$s type of boolean comparison CanGenerate == true, it is completely redundant, like multiplying a number by 1, there is no logical reason to do it. Second, you don't need to check if the number inside NOTG is greater than zero (NumberofObjectsToGenerate[i] > 0) because if you will use that number to iterate, but it is zero, there will simply be zero iterations and the code won't spawn anyt$$anonymous$$ng, it is a bit redundant to do that check too. And finally you can see that I didn't include the CanGenerate stuff, well that's because I don't completely understand where you were going with that so I didn't want to mess with it, you can include that part yourself.

Hope t$$anonymous$$s helps!

Edit: Corrected the Instantiate line

Comment
$$anonymous$$

People who like this

1 Show 6 · 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 $$anonymous$$ · Dec 14, 2017 at 01:07 AM 0
Share

CanGenerate was supposed to be a public variable, I don't remember why I set it to private. Also, I don't see where the foreach statement comes in to play.

avatar image MacDx $$anonymous$$ · Dec 14, 2017 at 02:40 AM 0
Share

Also, I don't see where the foreach statement comes in to play.

You don't need the foreach that's the whole point of what I said, the foreach logic won't work here because you need to iterate through the NOTG array with a for and then a second for to iterate the number of times each entry in NOTG dictates. There is absolutely no need for a foreach here, besides a foreach will not give you a position in the array, which is essential to this problem since you want to link both arrays by position. The code I posted produces 50 of the first and 100 of the second. But it looks like what I'm saying is falling to deaf ears, since you went and did the same (having the foreach, which will obviously not work).

avatar image $$anonymous$$ MacDx · Dec 14, 2017 at 03:17 AM 0
Share

The problem was that I was confused at GeneratedGameObject being used to instantiate when it has not even been created by the foreach statement, but I figured out that I had to use GeneratedObjects[i].

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EcoGeneraterScript : MonoBehaviour {
 
     public GameObject[] GeneratedObjects;
 
     public int[] NumberofObjectsToGenerate;
 
     public float MapWidth;
     public float MapLength;
 
     private float Cord_X = 0;
     private float Cord_Z = 0;
 
     void Update () {
         for (int i = 0; i < NumberofObjectsToGenerate.Length; i++) {
             int NumberOfInstances = NumberofObjectsToGenerate [i];
             for (int j = 0; j<NumberOfInstances; j++){
                 if (NumberofObjectsToGenerate[i] > 0 && Cord_Z < MapLength) {
                     Instantiate(GeneratedObjects[i], new Vector3(Cord_X, 0.5f, Cord_Z),Quaternion.identity);
                     NumberofObjectsToGenerate[i] -= 1;
                     Cord_X += 1;
                     if (Cord_X >= MapLength) {
                         Cord_X = 0;
                         Cord_Z += 1;
                         if (Cord_Z >= MapLength) {
                         }
                     }
                 }
             }
         }
     }
 }
Show more comments
avatar image $$anonymous$$ · Dec 14, 2017 at 01:17 AM 0
Share

This code should be slightly better, but it still does not generate 50 of on thing and 100 of another, just 150 of both.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EcoGeneraterScript : MonoBehaviour {
 
     public GameObject[] GeneratedObjects;
 
     public int[] NumberofObjectsToGenerate;
 
     public float MapWidth;
     public float MapLength;
 
     private float Cord_X = 0;
     private float Cord_Z = 0;
 
     void Update () {
         for (int i = 0; i < NumberofObjectsToGenerate.Length; i++) {
             int NumberOfInstances = NumberofObjectsToGenerate [i];
             for (int j = 0; j<NumberOfInstances; j++){
             foreach (GameObject GeneratedGameObject in GeneratedObjects) {
                 if (NumberofObjectsToGenerate[i] > 0 && Cord_Z < MapLength) {
                     Instantiate(GeneratedGameObject, new Vector3(Cord_X, 0.5f, Cord_Z),Quaternion.identity);
                     NumberofObjectsToGenerate[i] -= 1;
                     Cord_X += 1;
                     if (Cord_X >= MapLength) {
                         Cord_X = 0;
                         Cord_Z += 1;
                             if (Cord_Z >= MapLength) {
                             }
                         }
                     }
                 }
             }
         }
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

82 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

Related Questions

How to check if a vector 3 is in a vector 3 array c#. 1 Answer

random number generator problem 1 Answer

2D array generated with custom inspector, null when starting game? 1 Answer

Problem with object generation script? 1 Answer

Procedurally generated mesh JS - explain the error please 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