• 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 trriley · Sep 30, 2011 at 12:09 AM · arrays

arrays and remove at

This has been driving me nuts for a month. Here is the situation. I have 10 different colored objects on the screen all with a "sp" tag. I want to randomly choose one and make a copy of it on the screen. Only once for each object. all this happens on a collision.

function OnTriggerEnter(thing: Collider){ //
onelistWhites.clear();
listWhites = null;
nodesFixedArray=null;
nodesFixedArray = GameObject.FindGameObjectsWithTag("sp");
listWhites = new Array(nodesFixedArray);
i = Random.Range(0,listWhites.length);
var theObject : GameObject = listWhites[i];
var newobject = Instantiate (theObject) as GameObject;;
    listWhites.RemoveAt(i);
on collision I create a random object from the array. remove it from the array. but sometimes the next time i want to create a random object from the array, one of the old ones are still there and it instantiates it again.

It seems to me that the listWhites.RemoveAt(i); does not always work. or does not have time to work. is a batch of code in a function initiated one line at a time, or one the function reaches its end does it all fire at once?

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
3

Answer by aldonaletto · Sep 30, 2011 at 01:48 AM

The problem is that you're recreating nodesFixedArray and listWhites each collision, so deleting elements has no effect at all. You should create nodesFixedArray and copy it to listWhites at Start (or other convenient function), and place the rest of your code in OnTriggerEnter:

var onelistWhites: Array;

function Start(){ var spObjects: GameObject[]; spObjects = GameObject.FindGameObjectsWithTag("sp"); onelistWhites = new Array(spObjects); // copy spObjects to onelistWhites }

function OnTriggerEnter(thing: Collider){ var i = Random.Range(0,listWhites.length); var theObject : GameObject = listWhites[i]; var newobject: GameObject = Instantiate(theObject); listWhites.RemoveAt(i); // eliminate the object from the list ... } But you have other problems:
1- Instantiate(theObject) just clones the original object at its original position, so you will have both occupying the same space - you should specify the position and rotation of the clone to avoid this;
2- You must check the case when listWhites is completely duplicated and thus have no more elements.

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 trriley · Sep 30, 2011 at 12:44 PM 0
Share

I do have it repositioning, I simplified this for simplisity sake. I will give it a try. Thanks a million.

avatar image
-1

Answer by Connie.du · Oct 19, 2011 at 12:03 PM

hi i got a similar problem here.i m doing a puzzle game and i need to drag a clone object which was randomly array at the bottom of the scene. this is the script for drag and drop that i did

 static var clickedObj : Transform; 
 private var offSet : Vector3;     
 var cubewall : GameObject;
 private var snapAllowance: int = 5;
 var linkToArray : Array;
 
 function Update () {
     var ray = camera.ScreenPointToRay(Input.mousePosition); 
     if (Input.GetMouseButtonDown(0)) {    
         var hit : RaycastHit; 
         if (Physics.Raycast(ray, hit, Mathf.Infinity)) {      
             if(hit.collider.gameObject.tag == "Pipe") {
                 clickedObj = hit.transform;    
                 offSet = clickedObj.position-ray.origin;   
             }
         }
     }




and this is the code for array:

 var blockTypes: GameObject[];
 var inGamePos: Array = new Vector3[4];
 static var clickedObj : Transform; 
 var pos1: GameObject;
 var pos2: GameObject;
 var pos3: GameObject;
 var pos4: GameObject;
 
 function Start () {
     
     inGamePos[0] = pos1.transform.position;
     inGamePos[1] = pos2.transform.position;
     inGamePos[2] = pos3.transform.position;
     inGamePos[3] = pos4.transform.position;
     var blockTypesWeight= [2,2,1,1,1,2];
         
     for (i = inGamePos.length - 1; i >= 0; i--) {
         var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
         var newPipe: GameObject = Instantiate(ranPipe,inGamePos[i],Quaternion.identity);
         
             }
 }


i have tried but i couldnt click and drag the pipe that is random arrayed at the bottom even though when i put the pipe to the "Cubewall", i click and drag around perfectly fine. what i need to do to click and drag the pipe(clone) randomly arrayed at the bottom??

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 Eric5h5 · Oct 19, 2011 at 04:23 PM 0
Share

This isn't an answer. Please post it as a new question.

avatar image
0

Answer by trriley · Sep 30, 2011 at 06:00 PM

OMG Thank you so much. I did some rewrite and it all works just as i wanted. Thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Check/compare array elements 1 Answer

Is it faster to access an Vector3 array than to access a mesh's vertices? 2 Answers

Problems with Arrays 2 Answers

Assets/Scripts/Inventory.cs(9,40): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Inventory.differentItems' 1 Answer

Declaring jagged array in javascript 1 Answer


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