# Help With making something random but not totally random.

I am currently trying to make a script that allows my 4 turrets to shoot at different times. but I want the shooting order to change after every loop. I need a script that makes the shooting order random. but never have 2 turrets shooting at the same time. example:

if turret1 is shooting after 1 second. turret2 cant shoot after 1 second so it shoots and 3 seconds. turret3 cant shoot at 1 or 3 seconds so it shoots after 2 seconds…Next loop, turret1 shoots at 2 seconds. turret2 cant shoot at 2 seconds so it shoots at 1 second. turret3 cant shoot at 1 or 2 so it shoots at 3 seconds.

how would I make a script that does this action? my Turrets current script is:

``````var LookAtTarget: Transform;
var damp=6.0;
var BulletPrefab: Transform;
var savedTime=0;
var delayTime=2f;
var StartTime=3;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

var seconds: int = Time.time;

Shoot(seconds);
}
}
function Shoot(seconds)
{
if (seconds == savedTime)
{
var bullet = Instantiate(BulletPrefab ,transform.Find ("SpawnPoint").transform.position ,
Quaternion.identity);

savedTime=seconds+delayTime;
}
}
``````

I’m aware it isn’t super neat. but I’m new to coding and it at least shoots.
Having that, I tried multiple different methods of making a script that allows to do the action I want to. But I just cant make it happen. Can anyone please help? I’ve been working on this small problem for 3 days. (In javascript please)

Suppose this were a card game. You have one card for each turret. The algorithm would be simple: shuffle the cards, draw 1 card each second (and shoot that turret), and repeat. When the deck is empty, reshuffle the discards.

Now, sadly, C# doesn’t have a shuffle method built in. I’ve got one in my standard bag of tricks, but even more sadly, you’re using JS.

However, you don’t really need to shuffle — you need to draw one at random, which is easy to do. I don’t speak JS anymore, so I’ll just give it to you in pseudo-code, and you can write it up in JS. This all goes in the Update method:

• if it’s not time to shoot yet, return.

• check our list of turrets that haven’t fired yet; if it’s empty, initialize it with all our turrets.

• pick one at random. Fire that turret, and remove it from the list.

• Set the next-shoot-time to the current time plus one second (or whatever interval you like).

That’s it. Implement each of those four steps carefully, and you’ll have it! But if you have any trouble, post back here and get some help. And if you ever want to reexamine your life choices and make the switch to C#, never fear, we’ll help with that too.

I’d love to switch over to C# but from my knowledge (which at the currently as a vast as a subatomic particle) you can’t code a C# script to “communicate” with JavaScript. And although I do understand C# (but very little) I could not for the life of me translate my javascript to C# so that this would work. I’m open for all suggestions of course if you have any ideas please share and thank you!

Didn’t you ask the same thing here? Communication between scripts? - Unity Engine - Unity Discussions

indeed I did, I am new to Unity Forums and I thought maybe I could get another persons help if I made this. I just really want this to work and anyone able to solve it will be much appreciated.

Maybe so, but it’s not very polite. There are enough new posts in these forums already, and if anyone else finds the thread later it’s much better to have all the information in a single thread.

I apologize for offending you, and i appreciate your help a lot. I honestly just needed a second opinion because I cant get it working. it’s nothing against you I just figured if I made anew post someone would be able to help resolve our current issues. and i feel rude continually bothering you with errors. So i just wanted to get a second opinion.

No worries, its not a big deal and I’m not at all personally offended

So, how’s it going? I gave you the solution above; have you tried to implement it? On which step (if any) did you get stuck?