Need some help with some custom code - Its short

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.

  1. 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.

  2. 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.

Just to rephrase SparrowsNest’s point…

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.

2 Likes

No, where did you get this from?

You just need to assign each a unique position to each, @Antistone made it super clear what I meant.

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.

1 Like

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.

1 Like