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 RmurilloM · Apr 12, 2012 at 04:15 AM · c#arrayrandomcharacters

How to select an item from an GameObject array just ONCE?

Hi guys! I apologize if this questions have been already posted!.. But I guess I'm not good at searching!

Anyway, this is what I want to do: I created a GameObject[] array. I select the items of the array via Inspector. When my game is running, a GameObject (Character) is selected and it has to walk up to a target.position.

The problem I have, is that the character that have been already selected, is again selected! What I want to do is to avoid that! So, How can I select an item from an array just once? Please, C-Sharp!

Here is the code I have so far:

 using UnityEngine;

using System.Collections;

public class RandomCharactersMovement : MonoBehaviour {

 public GameObject[] Characters = new GameObject[5];
 public Vector3 Target = new Vector3(-18.03273f, 0.09936082f, 1.479195f);
 float smooth = 3f;
 public int TimeOut = 7;
 public GameObject characterChosen;
 
 
     
 // Use this for initialization
 void Start ()
 {
     RandomCharacter ();
 }
 
 // Update is called once per frame
 void Update ()
 {
         Invoke("MovingCharacter", TimeOut);
     
 }
 void MovingCharacter(){
     if (characterChosen.transform.position.z <= 1.910322) {
         characterChosen.transform.animation.CrossFade("walk");
         characterChosen.transform.Translate(Vector3.forward * Time.deltaTime * smooth);
     }
     else {
         characterChosen.transform.animation.CrossFade ("idle1");
         Invoke("GoBack", 10);
     }
 }
 void GoBack(){
 characterChosen.transform.eulerAngles = new Vector3(0,180,0);
 characterChosen.transform.animation.CrossFade ("walk");
 characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * smooth);
     Invoke ("DestroyIt", 5);
 }
 void DestroyIt(){
 GameObject.Destroy (characterChosen);
 }
 
 void RandomCharacter(){
     int characterIndex = Random.Range (0, Characters.Length);
     characterChosen = Characters[characterIndex];
 }

}

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

3 Replies

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

Answer by Kleptomaniac · Apr 12, 2012 at 06:30 AM

Yes, as @BLiTZWiNG said, you should use a List in a way known as a shuffle bag. This removes an element of an array after having being selected, so as to eliminate reselection.

To do this, at the start of your script (after the line using System.Collections; add this line:

 using System.Collections.Generic;

That allows you to use the Generic class. Then, replace your Characters variable with:

 public List<GameObject> Characters = new List<GameObject>();

From there you can add all your character gameObjects to the new List Characters in the inspector. And then finally, at the end, rewrite void RandomCharacter() as:

 int characterIndex = Random.Range (0, Characters.Count);
 characterChosen = Characters[characterIndex];
 Characters.RemoveAt(characterIndex);

Hope that helps, Klep

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

Answer by Talimar · Apr 12, 2012 at 05:46 AM

You should use a List and then once you select a cahracter, remove it from the list.

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

Answer by RmurilloM · Apr 12, 2012 at 07:07 AM

That's awesome!! Thanks so much your answers!!

I'll try it as soon as possible and I'll post you the results!

Thanks again Guys!.. I'm starting to like this site a LOT!

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# array equal to another array minus one entry. 1 Answer

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Accessing Instance Bool from other script 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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