What are my errors in the following code? [closed] - unity3d

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is the function:
for (i = 0; i <= array.Length; i++) {
if (array[i].transform.position = 0)
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
else if (array[i].transform.position > 0)
array[i].transform.forward = Vector3(1,0,0);
}

I'm not sure if this is a valid question but there is for sure some logic errors :
First of all i'm not sure there is a RemoveAt(int index) for arrays
(but i'm not a big unityscript user) (there is for List though)
You should absolutely never (even if some weird languages maybe allow it to you) try to access an object you just deleted... which is what you try to do here :
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
Position is a Vector3 NOT a int or float so you cannot do : array[i].transform.position = 0
You should never use the = (assignment operator) in an if() you should use the == (comparison operator) (because = returns always true when assignment is possible)
That line is wrong for the same reason as before array[i].transform.position > 0
array[i].transform.forward = Vector3(1,0,0); Leaves me wondering because if it was C# i'd try the new keyword before Vector3() and i'd prefer floats that way :
array[i].transform.forward = new Vector3(1.0F,0,0); But even there Unity will throw you an error stating that you cannot modify components of Transform without making a copy first i believe...
But nice try :D

you can't compare a vector to 0 it needs to be like this
if(myObject.transform.position==Vector3.zero)
and for removing things I'd suggest to include System.Collections.generic lib
then use Listarray then you can use array.RemoveAt(index);

Related

Passing by reference and assigning into a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was studying about passing by reference. It made me wonder what would happen in the following example (Written in pseudo-C which supports "by reference"):
int foo(int a) {
a = 5;
return a * 2;
}
int main() {
int a = 1;
a = foo(a);
printf("%d",a);
return 0;
}
What should be printed? if we only did foo(a); without assigning into a then we would get 5. but what would be printed when assigning? should it be 5 or 10?
Since you have a = foo(a); in your main() function, a will contain the result returned by foo(a). foo(a) will always return 10 no matter what a is.
C does not support pass by reference. Changing a = foo(a); to just foo(a); would mean a would retain the value it had before it was passed to foo(), so it would be 1.
One variation of C that supports pass by reference is C++. In C++, you could write foo() as:
int foo(int &a) {
a = 5;
return a * 2;
}
The int &a syntax is used to denote that the parameter will be passed by reference. Now, foo will assign 5 to the referenced variable, but still always return 10.
In this case a = foo(a); will result in a having the value 10, and foo(a); alone will result in a having the value 5.

Modify variable in for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The code below would print abc 5 times and then print 1024. As far as I understand, in any for, the "iterator " is automatically declared (the equivalent of a C(++)/Java for(int i=1; i<=5; i++) ). Is it possible to actually not automatically create that variable and use the one declared before the for so that it would print abc 5 times and then print 5, thus modifying it?
var i = 1024
for i in 1...5 {
print("abc")
}
print(i)
#DrummerB's answer works, but if you want a for...in loop, this will also work. It's the same principle - declare your variable outside the loop and increment it inside it:
var i:Int = 0
for _ in 0...5 {
print("abc")
i += 1
}
print(i)
Since you aren't referencing a loop variable, Swift syntax recommends an underscore.
You could just rewrite the for loop as a while loop like this:
var i = 1024
i = 1
while i <= 5 {
print("abc")
i = i+1
}
print(i)
If you really want to change the value of the i using the loop in that way, you can do:
var i=1024
for j in 1...5 {
print("abc")
i = j
}
print(i)
The j would be used strictly in the loop, so once finished, it's value is dumped. But a variable declared before (i in your case), could take it's value and maintain it.

Why use variadic parameters [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am struggling to see if there is an obvious advantage over which method to use when passing values into a function. My code below may not be the best example to explain the decision I'm trying to make, but it is, in my opinion, the easiest to understand.
Variadic Parameter Approach
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean(5, 10, 15)
Array Parameter Approach
func arithmeticMean(numbers: [Double]) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean([5, 10, 15])
Is either of the two techniques preferred? If so, why (speed, reliability or just ease of reading)? Thanks.
I think there is no speed difference.Because,inside the function,you use Variadic Parameter just as Array.
I think that if the parameters count is small,for example,less than 5,Variadic Parameter may be a better solution,because it is easy to read.
If the count of parameters is large. Array is better solution.
Also know that,Variadic Parameter have some limitation:
A function may have at most one variadic parameter, and it must always appear last in the parameter list, to avoid ambiguity when calling the function with multiple parameters.
If your function has one or more parameters with a default value, and also has a variadic parameter, place the variadic parameter after all the defaulted parameters at the very end of the list.
Just from my idea.Hopes helpful

Why dosn't this objective-c if statement work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I's sorry but I'm new to Xcode and coding.
Im trying to create an If statement so that if label 3 = or greater then 20 label 4 = 0, else if lower then 20 it = 1 and if lower then 18.5 its = 2.
this is my code for that:
if (_label3.text >= #"20") {_label4.text = #"0";}
else if (_label3.text < #"20") {_label4.text = #"1";}
else if (_label3.text <= #"18.5") {_label4.text = #"2";}
I'm not sure what is going wrong, but i am getting this error 'Direct comparison of String literal has undefined behavior' and Xcode wont let me build the app.
Thank for your Help
you are using arithmetic operations on strings. that for sure makes no sense
create a float from the textfield input
if ([_label3.text floatValue] >= 20.0) {_label4.text = #"0";}
aslo you have to change the 1st and 2nd else branch, a the last one will never be called, as if it is true, the fist one is also be true.
float value = [_label3.text floatValue];
if (value > 20.0) {_label4.text = #"0";}
else if (value <= 18.5) {_label4.text = #"2";}
else if (value < 20.0) {_label4.text = #"1";}
label.text is string value and you cant compare string to int. Change the string to int and then compare like this
if([_label3.text intValue] >= 20){
_label4.text = #"0";
}
Hope this helps.

How to write this snippet more clean [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In a turn-based game, I'd like to find the previous player.
To find the next player, I can just type:
int lastPlayer = match.currentPlayer - 1;
The problem is when the currentPlayer is player 1. Then lastPlayer becomes 0, which is wrong. It should be player6.
To fix this, I can do:
int lastPlayer = match.currentPlayer - 1;
if (lastPlayer == 0)
lastPlayer = match.numberOfPlayers;
My question is how to write this in a cleaner way. I know game center, turn based code do something like:
(currentIndex + 1) % match.participants.count];
How can I rewrite my code to do the same?
Thanks in advance
How about that?
int lastPlayer = match.currentPlayer > 1 ? match.currentPlayer - 1 : match.numberOfPlayers;
If you have a turn counter like this:
for(int i=0;i< maxTurns;i++){//do something}
and player1 plays the 1st turn, player2 the second and so on,
you can get the current players number exactly as its done in gamecenter.
Maybe you don't know the modulo "%" operator,
it returns the rest of a division, for example
3%2 = 1
your turncounter keeps counting up so the next player is
(turncounter + 1)%match.numberOfPlayer
in this case the modulo operator kind of "resets" your counter when it reaches the max player count (its round 6 and there are 5 players 6%5 = 1).
To sum it up, currentPlayer is turncounter%match.numberOfPlayer
nextPlayer is ++turncounter%match.numberOfPlayer