- Home /
Substute for Array.length?
I am translating a script from the Unity Wiki from Javascript to C#. The other scripts I have got working properly, but I've run into a few problems here that I have not been able to find a work around for. I get three errors for which I cant think of a way to fix:
Assets/First-Third Person Framework/Scripts/GunParticles.cs(13,28): error CS0119: Expression denotes a
type', where a
variable',value' or
method group' was expectedAssets/First-Third Person Framework/Scripts/GunParticles.cs(26,57): error CS1061: Type
System.Array' does not contain a definition for
length' and no extension methodlength' of type
System.Array' could be found (are you missing a using directive or an assembly reference?)Assets/First-Third Person Framework/Scripts/GunParticles.cs(28,34): error CS0021: Cannot apply indexing with [] to an expression of type `System.Array'
The converted code -
using UnityEngine;
using System;
using System.Collections;
public class GunParticles : Gun {
private bool _cState;
private Array emitters;
// Use this for initialization
void Start () {
_cState = true;
emitters = Gun/capsuleEmitter;
ChangeState(false);
}
public void ChangeState(bool p_newState)
{
if(_cState == p_newState) return;
_cState = p_newState;
if(emitters != null)
{
for(int cnt = 0; cnt < emitters.length; cnt++)
{
(emitters[cnt] as ParticleEmitter).emit = p_newState;
}
}
}
}
Any suggestions would be greatly appreciated. Thank you.
Answer by Benproductions1 · Jul 22, 2013 at 01:45 AM
Array
as such, only exists in Javascript. And even there it should never EVER be used. Use Generic Lists instead
Actually, if you call "using System;" you get the Array just as that. Default you can use ArrayList in C# without using the system call. I agree with you that there are better ways than using an Array.
The problem with Array
is that it is basically a slow List<object>
. There is no point to it's existence and it should be deprecated :)
In that case, I'll have to play around with List sometime and see how I like it. Thanks for the advice! :)
Answer by Adamcbrz · Jul 22, 2013 at 01:22 AM
1) I need more context for what this line is doing. You may need a get component or find
2) c# Array.Length not lowercase
3) really you need to declare the array like private ParicleEmitter[] emitters.
See if you fix those then let me know what errors you get.
1) This script inherits from another script I translated that allows the user to create various guns with different powers, ranges...ect.
2) Using Array.Length still gives off the error that System.Array can't apply indexing
3) In the inherited script I have the variable ParticleEmitter[] capsuleEmitter;
Thanks for the assistance, as soon as i get the kinks worked out I will post the C# version back on the UnityWiki for anyone to use.