Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ChrisTylr · Oct 01, 2013 at 08:58 AM · arrayrandomspawnempty game object

Spawning targets on 2 of 4 pre-defined locations using an array and empty game objects?

I am trying to spawn a target on pre-defined locations which will be the locations of empty game objects.

But i only want two targets spawning on the two of the four empty game objects i have.

From my understanding, the best way to do this would be to set up an array for the empty game objects and put the array though a loop that will randomly pick two numbers between 0 and 3, which will be linked to two empty game objects through the array. then spawn a target on that location, or have a target already there but just turn the renderer on for that target when the corresponding targets are picked.

At the minute all i have is one line of code that can turn a targets renderer on when true, and off when false. My initial intention was to write a new script (targetmanager) that will do the above, and then call into this other script and turn the false to true.

Ill have no trouble accessing this renderer script, I'm just having trouble getting my head round the 'randomly generating two numbers, linking these numbers with objects in an array and then making the whole thing work' part....

This may not be the correct way to do things and so any input provided will be taken on-board and no doubt prove useful in some way as i have no code for this yet.

Please try and be forgiving, im new-ish to programming, also i am using Java

Thank-you in advance

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fattie · Oct 01, 2013 at 09:11 AM

You are correct that you should have four "marker objects" (simply, empty game objects) which are the positions.

If I'm not mistaken you're just wondering the basic programming question of how to pick two of these randomly, is this correct?

I will actually go ahead and tell you the correct programming solution for this - so it's a world first for this list.

So you have the index numbers zero through three. You're with me so far right?

Notice there are four (count) such numbers.

 var howManyNumbersDoIHave : int =  4;

No problem so far right?

Now your solution will be TWO INDEX numbers. Correct right?

 var theFirstResultIndex : int
 var theSecondResultIndex : int

The first one is easy.

 theFirstResultIndex = Radom.Range( 0, howManyNumbersDoIHave );

{Aside -- Note that with Random.Range, the second argument IS THE TOTAL COUNT, it is not the "last number you want". So for example Random.Range(931, 1550) would START at 931, and there would be a possibility of 1550 numbers in total. So note that Random.Range(0,2) would give you either a "0" or a "1". So hopefully that's clear.}

Now here's how you do the second one. You travel in a loop around from where you are (the first one) but only as far as you don't pass the first one again.

 var howFarToTravelAtMostInALoop : int;
 howFarToTravelAtMostInALoop = howManyNumbersDoIHave - 1;

Makes sense right?

So you need a random number from ONE to and including howFarToTravelAtMostInALoop.

var howFarToTravel : int;

Let's pick a random number from 1 inclusive up to and including howFarToTravelAtMostInALoop

 howFarToTravel = Random.Range( 1, howFarToTravelAtMostInALoop );

In your example this will give you either a 1, 2 or a 3

 var whereILandedAfterTravellingInALoop : int;

Here's a big secret.

 whereILandedAfterTravellingInALoop =
     theFirstResultIndex + howFarToTravel;

but that could "go past the end" am I right? So you have to do this:

 whereILandedAfterTravellingInALoop =
    whereILandedAfterTravellingInALoop % howManyNumbersDoIHave;

 theSecondResultIndex = whereILandedAfterTravellingInALoop;

Notice the magical "modulo" (arguably, "modulus") symbol. You can learn about this easily by doing a four-year computer science / pure math double major course, so go do that.

All you need to know is it will make you travel in a loop.

Et Voila, you have the two answers:

 Debug.Log("the two safe and different numbers are "
  + theFirstResultIndex +" "+ theSecondResultIndex);

Any other method for doing this is wrong. Hopefully nobody ever has to explain this again in this universe, and i hope it helps!


Regarding how to turn on and off the array, it's this simple ..

"set up an array for the empty game objects..." yes, that's correct

var myFourItems : GameObject[];

in the editor, drag the four items there. Then just do this

 myFourItems[theFirstResultIndex].SetActive(false);
 myFourItems[theSecondResultIndex].SetActive(false);

(or, if you wanted, turn the renderer on/off .. whatever is relevant to you.)

It could be you further more have complicated functions on those objects, so your script might be called "MyScript.js" which is attached to each of the four objects and which has functions like "MyFunction()". To access those, just do this...

 myFourItems[x].GetComponent<MyScript>.YourFunction();

Relatedly, I also direct you to this long answer which is a basic in stuff like this; some of the example code for "accessing stuff in Lists etc" may be relevant here. http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

Comment
Add comment · Show 1 · 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 ChrisTylr · Oct 01, 2013 at 10:29 AM 0
Share

Thank you very much....

In reference to taking a course on this, I am currently doing a 3 yr games design course but have only just started scripting.

So as soon as I'm done in today's lectures I will give your answer a go

Thank you for making it simple, loving those variable names :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Spawn object using vector array anywhere but previous spawn location 1 Answer

Spawning Objects Using An Array. 1 Answer

Spawn object from Random Vector3 in array 0 Answers

How do I Spawn a random gameObject from Array1, at a random position of gameObjects from Array2 ? 1 Answer

How to fix some of location have more than 1 object? 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