- Home /
Delete duplicate elements from an array in UnityScript
If you find my question as a duplicate question, please post the link to the answer also, I didn't find a proper solution to my problem here. That's why the question is asked below. My problem is given in the code below:
var Speed1:float;
var Speed2:float;
var Speed3:float;
var Speed4:float;
var MaxStateTimer:float;
var MinStateTimer:float;
private var StateArray:Array;
private var StateTimer:float;
private var TaskValue1:float=70;
private var TaskValue2:float=90;
private var TaskValue3:float=100;
private var TaskValue4:float=100;
function Start()
{
StateArray=new Array();
StateTimer=Random.Range(MinStateTimer,MaxStateTimer);
}
function Update()
{
TaskValue1=Speed1*Time.deltaTime;
TaskValue2=Speed2*Time.deltaTime;
TaskValue3=Speed3*Time.deltaTime;
TaskValue4=Speed4*Time.deltaTime;
StateTimer-=Time.deltaTime;
if(StateTimer<=0)
{
StateTimer=Random.Range(MinStateTimer,MaxStateTimer);
if(TaskValue1<=60)
{
//I need to add a number 1 to the array StateArray.
}
if(TaskValue2<=60)
{
//I need to add a number 2 to the array StateArray.
}
if(TaskValue3<=60)
{
//I need to add a number 3 to the Array StateArray.
}
if(TaskValue4<=60)
{
//I need to add a number 4 to the array StateArray.
}
//1) Now from these numbers in the array, I need to remove all the duplicate elemets in the array.
//After removing duplicate elements, I need to randomly select a number from the list of numbers in the array StateArray
// if the randomly selected number is 1, i have to do task1 if random number selected is 2 do task2 if random number selected is 3 do task3
//
//I tried several methods but all were giving errors. I need to know how to remove duplicate elements from the StateArray.
//After removing duplicate numbers from the StateArray, ho do I select a random number from the Array.
}
}
Comment
Best Answer
Answer by fafase · Jul 04, 2015 at 10:04 AM
Simple way:
bool a,b,c,d;
if(TaskValue1<=60)
{
if(a == false){
//I need to add a number 1 to the array StateArray.
a = true;
}
}
// Same for others with appropriate boolean.
Thanks for the answer. One more doubt, how to select a random number from the array StateArray?
var item = StateArray[Random.Range(0, StateArray.Length - 1)];
If StateArray is a list then StateArray.Count - 1.
Yeah.....mmmmright....Let's see what you may have wrong there....Ah... got it, those two lines won't compile:
"I'm new to Unity and I don't understand the working of code in Update function."
"Because if I performed well, then only I will be getting a job in a company."
Your answer
![](https://koobas.hobune.stream/wayback/20220611225736im_/https://answers.unity.com/themes/thub/images/avi.jpg)