- Home /
Question by
digitaldebonaire · Mar 10, 2014 at 04:27 AM ·
c#javascripttranslate
translate javascript for loop to C#
I am trying to implement a function, I'm following a tutorial written in javascript and trying to translate it to C# as an exercise.
function FireProjectile()
{
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
CalculateAimError ();
for( theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
That is it in javascript. Here is my attempt in C#, notice my issue with the for loop
void FireProjectile()
{
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
CalculateAimError ();
for( theMuzzlePos; muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
How do I translate this correctly?
Comment
Best Answer
Answer by kestik · Mar 10, 2014 at 04:30 AM
In c# its 'foreach' loop. Example:
foreach(Transform theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
Answer by getyour411 · Mar 10, 2014 at 04:31 AM
A standard for-incremental loop might look like this for a List/Array, not sure if that's what you have
for(int cnt=0; cnt < muzzlePositions.Count; cnt++)
Instantiate(myProjectile,muzzlePositions[cnt].position,muzzlePositions[cnt].rotation;
Use .Count or .Length depending on the type.
Your answer
Follow this Question
Related Questions
Camera Effect + Translating US to C# 3 Answers
Translate code from C# to Javascript 1 Answer
translate C# with => to unityscript 2 Answers
Converting Variable to C# 1 Answer
Create new plane and Raycast in C#? 1 Answer