using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProceduralRoomSpawner : MonoBehaviour
{
[SerializeField] GameObject[] roomPrefab;
Vector3[] newPos = new Vector3[4];
private void Start()
{
newPos[0] = new Vector3(1, 0, 0);
newPos[1] = new Vector3(-1, 0, 0);
newPos[2] = new Vector3(0, 0, 1);
newPos[3] = new Vector3(0, 0, -1);
// a random number between 0 and 3 used to choose random array member
int randomRange = Random.Range(0, 4);
var i = 0;
for (i = 0; i < roomPrefab.Length; i++)
{
Instantiate(roomPrefab[i], newPos[randomRange], Quaternion.identity);
}
}
}
So I have 5 generic 3d cubes in my roomPrefab array. I have a couple of questions.
Why are they all spawning in the same place? I set the position to instantiate at a Vector3 using a random number between 0 and 3 in the array. It should then pick a random vector out of my vector3 array to use to place each cube as its running through the for loop. Iâm almost positive Iâm just not quite understanding whats going on.
I want to be able to almost make a line going from cube to cube. And it can choose to go in 4 directions from the last placed cube. My thinking is that I need to store each cubes position in a variable. Then add one of my newPos[ ] vector3âs to the next cubes position but I cannot for the life of me even write the psuedocode for it. Maybe its late and my brain is fried. Maybe This is just above my skill level atm. Not sure. Any help would appreciated. Would love even someone psuedocode and I could figure out the rest.
Youâre picking one random vector and using that.
youâre probably thinking
private void Start()
{
newPos[0] = new Vector3(1, 0, 0);
newPos[1] = new Vector3(-1, 0, 0);
newPos[2] = new Vector3(0, 0, 1);
newPos[3] = new Vector3(0, 0, -1);
// a random number between 0 and 3 used to choose random array member
for (int i = 0; i < roomPrefab.Length; i++)
{
Instantiate(roomPrefab[i], newPos[Random.Range(0, 4)], Quaternion.identity);
}
}
*not anything to stop you selecting the same position twice.
** notice the loop call, you can just declare the variable inside it, you donât need a special line for that.
You are correct, there is nothing stopping me from picking the same position twice. But Every single time this is run, all blocks are placed in the same exact position. There is never a block that isnât in the same place. Its not just the random giving me bad rng.
See the code i posted with the fixed, you generated one random number before the loop and used it instead of generating a number each iteration of the loop.
In this line of code: int randomRange = Random.Range(0, 4);
You seem to be imagining this means âfrom now on, whenever I write ârandomRangeâ, pick a random number from 0 to 4â.
What it actually means is âpick a random number between 0 and 4 right now, one time only and store that single, unchanging number in the variable ârandomRangeâ.â
If you want a different random number for each cube, then you need to call Random.Range() separately for each cube.
If thats that unclear to you I suggest you go back to basics with C#, once you get a better understanding of C# (and code in general) read this thread again.
Itâs a typical noob error that shows the assumption commitment fallacy.
âThe code is obvious to me, so why doesnât it work as I assume it should. If I was a compilerâŚâ
Donât be offended, I donât mean it as an insult. Itâs just you should really establish this pattern of thought here. Whenever this happens youâre ought to ask yourself âwhat did I wrongly assume if this is behaving so completely differently?â
Go step by step if needed.
Itâs a simple ego trip that arises from limited perception, because youâre absolutely sure it is simple enough to just rely on your intuition. Even though I have more experience, we are all just humans and I can definitely relate. Try to lower down your insistence in being 100% right in your expectations (especially in simple scenarios such as this, because thatâs always the trap) and youâll improve your problem-solving abilities, that I promise.
oh noâŚI understand the problem. Fixed it. I didnât have a problem not figuring out the problem, or my logic or anything like that. I was wrong. Thats easy to admit. But to say âmade it super clear what I meant.â is egotistical in and of itself if you want to really get into this and is not a good way to help a beginner. Or anyone really. You could try explaining it in a different way or even go even more barney style and dumb it down even more. But to say âits super clearâ is ego.
Iâm a complete beginner. Easy to say. There is no ego problem here.
I donât know you so Iâm generalizing. It becomes worse the more you know, btw. The assumptions. The idea that you cannot mess up something trivial. In fact, it still happens to me after 30 years of coding. The other day I had to rewrite an entire class because it was literally something only a drunken person would write. Astonishing. Two hours of work. But I was wise enough not to try and test it immediately (and endlessly frustrate myself), but left it for the morning version of me.
Iâm pretty sure Iâm missing some context. It seems like some posts were deleted.
But the only âsuper clearâ comment I see is one poster saying that a different poster made something super clear, so that seems unlikely to be ego. Heâs not complimenting himself. Sounds more like frustration.
When asking for help, I think itâs worth remembering that you are requesting a favor. If it doesnât work out, then no matter whoâs âfaultâ that is, youâre the one whoâs going to be stuck with the original problem.