I’m a complete newbie, working through the Unity Learn program. I’m working on my prototype game, and I’m having a ton of trouble figuring out how to write the SpawnManager script I need. I really want to learn, so I just want to know what I should research in order to figure this out… or a nudge in the right direction. I’ve been trying to figure it out on my own, but I’m lost at this point.
Anyway, the issue is with “enemy” spawns. I need them to spawn in sets in a row. Essentially one of the objects in the set can be broken, the others can’t. They move top to bottom on the screen and the player breaks them. In the example below, X would be what can be broken, the ^ would be the player.
[ ] [ ] X [ ] [ ]
[ ] [ ] [ ] X [ ]
[ ] [ ] [ ] X [ ]
^
I can’t figure out how to spawn the objects in those sets and randomize the position of the breakable wall (X) in that set.
This generates a row of booleans. False = unbreakable, True = breakable, with one True value randomly in the list. You can set the number of row elements in the Inspector, then hit Space to generate.
From this you’ll need to apply it to spawning your enemies, so they may have different appearances based on breakable or not, different behaviors, etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class forumArrayObjects : MonoBehaviour
{
[Range(2, 20)]
public int numEnemiesPerRow = 5;
void Update()
{
// hit Space to generate row with one breakable enemy ("false")
if (Input.GetKeyDown(KeyCode.Space))
{
bool[] enemyArray = new bool[numEnemiesPerRow];
for (int i = 0; i < numEnemiesPerRow; i++)
{
enemyArray[i] = false; // enemy can't be broken
}
int randIndex = Random.Range(0, numEnemiesPerRow);
enemyArray[randIndex] = true; // breakable enemy
print(string.Join(" ", enemyArray));
}
}
}
Thanks so much. I currently have the game functioning kind of like the first way you described. I’m basically trying to make a study aid for my kids, and maybe something that the teachers in his school can use in lieu of some of the awful educational “games” they use in the distance learning curriculum. Essentially trying to create a few simple minigames teachers can feed study data into, and the students have to know that data in order pass through the game. I know that’s a complicated endeavor for a newbie, so I’m breaking it into simple steps.
The first game is basically a very crude, top down space shooter, but rather than enemy aliens or asteroids, it’s question and and answer sets, that basically form obstacles the player can only pass through if they shoot through the correct answer. I have it working kind of, but it’s built in such a way that the level is completely predesigned. The Q/A sets and obstacles are basically predesigned game objects that spawn in a random order. The problems with this are that the location of the breakable answer obstacle is predetermined and not random and that each question and answer set has to be constructed out of game objects in Unity. I’m trying to work toward a system where the spawner pulls from two data sets (right answers and wrong answers) and populates my question and answer prefabs with that data, then spawns them as a horizontal set creating an obstacle. The goal is to allow someone to easily input data to the right answers and wrong answers data sets so they can quickly create an interactive study aid for their classroom.
Right now the problem I’m working on is just simply how to get my right and wrong answer sets to be random, essentially spawning 4 objects with bool values, allowing one of them to be destroyed. Eventually, I’ll add consequences for shooting the incorrect answer, like doing damage to the player object or spawning an enemy, etc.
The code I posted does that. Basically, just populate your “answer row” with bools that all are false, then randomly select one and flip it to true. Then you replace the false ones with incorrect answers and the true one with the correct answer (strings, and update the gameObjects to match visually) As long as you have enough incorrect answers in the bank, you won’t repeat them, and you won’t need to shuffle anything.
That said, shuffling is a perfectly good way to do it too! Make your List of strings like this:
[right answer, wrong answer 1, wrong answer 2, wrong answer 3] // etc for however many you want
then shuffle the List. The only issue is tracking which is the correct answer. You can check this by comparing strings once the player selects/shoots the one they want.
public static List<int> randomizeList(List<int> randList) // take list in, randomize order
{
for (int i = 0; i < randList.Count; i++)
{
int temp = randList[i];
int rand = Random.Range(i, randList.Count);
randList[i] = randList[rand];
randList[rand] = temp;
}
//Debug.Log("randList = " + string.Join(",", randList));
return randList;
}
Thanks so much Jay. I’m going to go over your code and look into shuffling algorithms until I feel like I understand what’s going on and can make a decision as to what will work best. I really appreciate you taking the time to help me with this. Hope you’re having a great day.