Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed May 21, 2019 at 06:19 PM by Mike_Honcho for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Mike_Honcho · May 21, 2019 at 07:22 AM · script.unity 2drandomize

Randomly instantiate objects of an array, but each only once. The Object you want to instantiate is null.

Hello, I'm kind of new to Unity and here I just can't figure out why it gives this error in InstObstacles().


In this I am trying to instantiate groups of obstacles.


Per island I want to instantiate at most one obstacle of the same group, thats why I try to set the whole group to null and then let a non-null object be chosen for instantiation. But the only object being instantiated is always the one in SetUp().


Here is the relevant code if something is unclear I will provide more information.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GroupSpawner : MonoBehaviour
 {
 float yy;
 int obstaclesAmount;
 int randomDimension;
 int randomNumber;
 int noLoop;

 public GameObject a1;
 public GameObject a2;
 public GameObject a3;
 public GameObject b1;
 public GameObject b2;
 public GameObject b3;
 public GameObject c1;
 public GameObject d1;
 public GameObject e1;
 public GameObject f1;
 public GameObject g1;

 GameObject[,] Obstacles;

 private void Awake()
 {
     Obstacles = new GameObject[7,3];
 }

 void Start()
 {
     yy = yy + 10;
 }

 void Update()
 {
     SpawnGroups();
 }


 public void SpawnGroups() //set the positon and amount of obstacle spawn, call Instantiate
 {
     if ((yy - gameObject.transform.position.y) < 50)
     {
         float distance = Random.Range(3, 13);   //distance between the groups
         obstaclesAmount = Random.Range(1, 4);   //obstacles per group
         yy = yy + distance;
         SetUp();
     }
 }
 void SetUp() //include all objects, choose and instantiate random, call Instisland
 {
     GameObject[,] Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };
     randomDimension = Random.Range(0, 7);
     randomNumber = Random.Range(0,3);

     Object.Instantiate(Obstacles[randomDimension, randomNumber], new Vector3(lw.transform.position.x + 0.7f, yy, lw.transform.position.z), Quaternion.identity);
     //This gets instantiated fine
     InstObstacles();
 }
 void InstObstacles() //exclude already spawned obstacles, choose obstacle and instantiate it, repeat x times
 {
     Obstacles[randomDimension, 0] = null; Obstacles[randomDimension, 1] = null; Obstacles[randomDimension, 2] = null;
     


     randomDimension = Random.Range(0,7);
     randomNumber = Random.Range(0,3);

     Debug.Log("test " + Obstacles[1,1] + Obstacles[2,0] + Obstacles[4,0] + Obstacles[6,0]);
     //this writes just "test" in the log, which means these are all null right?

     noLoop = 100;

     while (Obstacles[randomDimension,randomNumber] == null)
     {
         randomDimension = Random.Range(0, 7);
         randomNumber = Random.Range(0, 3);

         noLoop--;
         if (noLoop == 1)
         {
             Debug.Log("infiniteWhileLoop"); //gets called every time
             break;
         }
     }

     Debug.Log(Obstacles[randomDimension, randomNumber]);
     //This writes "Null" tho

     yy = yy + 1;
 //this gives the error
     Object.Instantiate(Obstacles[randomDimension, randomNumber], new Vector3(lw.transform.position.x + 0.7f, yy, lw.transform.position.z), Quaternion.identity);

     if (obstaclesAmount > 1) //it should call the function again to instantiate another obstacle for the group if true
     {
         obstaclesAmount--;
         InstObstacles();
     }
 }
 }
Comment
Add comment · Show 7
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 Bonfire-Boy · May 20, 2019 at 11:24 AM 0
Share

What's unclear is if/how all these variables get assigned (or even where they're declared)

{ a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { f2, f2, f2 }

The most obvious diagnosis is that they're all null, so all the elements in your array are null...

avatar image Mike_Honcho Bonfire-Boy · May 20, 2019 at 04:57 PM 0
Share

They are actual objects being set in the editor. I updated the code and added more comments for better understanding.

avatar image xxmariofer · May 20, 2019 at 11:24 AM 0
Share

im having issues to understand your code, obstacles and islands are the same? also the switch is no needed at all and can be changed to:

  Obstacles[randomDimension, 0] = null; Obstacles[randomDimension, 1] = null; Obstacles[randomDimension, 2] = null;

try changing the break from iunside the

 if(noLoop == 1) 

to a

 return;

since thats probably giving you the error(altough there might be another bug hard to tell without trying it out)

avatar image Mike_Honcho xxmariofer · May 20, 2019 at 04:52 PM 0
Share

I refined the code now and your version ins$$anonymous$$d of the switch statement is way better, but that doesn’t change anything. The "noLoop" is just there to avoid unity being stuck in the loop forever. An island (changed to "group" now) should be a group of obstacles in one place.

avatar image Bonfire-Boy · May 20, 2019 at 06:01 PM 1
Share

Can't post as answer atm because question is awaiting moderation, but I think I spotted the error.

At line 55 you declare a local variable. So you never actually populate the Obstacles member variable which you use in the InstObstacles() function. The local variable can be used within the SetUp() function (which is why line 58 works), but nowhere else.

avatar image Mike_Honcho Bonfire-Boy · May 20, 2019 at 06:28 PM 0
Share

But I can't just write Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } }; ins$$anonymous$$d.

avatar image Bonfire-Boy Mike_Honcho · May 20, 2019 at 07:38 PM 1
Share

Sure, but this should work...

Obstacles =new GameObject[7,3]{ { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };

(and the initialisation in Awake() becomes superfluous)

1 Reply

  • Sort: 
avatar image
0

Answer by Meishin · May 21, 2019 at 08:20 AM

Hello @Mike_Honcho, I'm no expert in C# so sorry if i'm wrong, but didnt you :

1) declare Obstacles array in your class

2) declared a 2nd Obstacles array and assigned your gameObjects in it in SetUp

3) Use Obstacles array declared in you class in IntsObstacles(), which has not been assigned

What happens if you remove the declaration in SetUp ?

 Obstacles = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c1,c1 }, { d1, d1, d1 }, { e1, e1, e1 }, { f1, f1, f1 }, { g1, g1, g1 } };
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 Bonfire-Boy · May 21, 2019 at 08:45 AM 0
Share

Read the comments. The OP has already been told this, tried your fix, replied that it doesn't work, and been provided with an alternative.

avatar image Meishin Bonfire-Boy · May 21, 2019 at 09:07 AM 0
Share

Ay yeah sorry i'm new to the forum i didnt see those comments :)

Follow this Question

Answers Answers and Comments

122 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

Related Questions

I'm trying to make scorezones but i cant seems to get it right.,Im trying to make a scorezone that you can go through and that can add 1 point to the player. 0 Answers

How do I access a script from a class inside a script. 1 Answer

Object disappears on running 1 Answer

How to give player a "dash" ability? 0 Answers

How to fix this Score UI problem 2 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