- Home /
Unknown identifier: 'types'. js(38,61)
Im pretty sure 'types' is defined but apparently it isnt
#pragma strict
public class weapon extends overWorld
{
public class weapon
{
private var ammo : int;
private var damage : int;
private var numberOfBullets : int;
private var firetype : int;
private var range :int;
private var weaponType : types;
public function weapon()
{
ammo = 0;
damage = 0;
numberOfBullets = 0;
firetype = 0;
range = 0;
weaponType = types.pistol;
}
public function weapon(amm : int, dmg : int, numOfBullets : int, rateOfFire : int, rng : int, typ : types)
{
ammo = amm;
damage = dmg;
numberOfBullets = numOfBullets;
firetype = rateOfFire;
range = rng;
weaponType = typ;
}
public enum types {sword, pistol, shotgun, machinegun, semiauto, sniper};
}
}
public var sniper : weapon = new weapon(5, 5, 5, 1, 5, types.sniper);
Debug.Log(sniper);
Answer by Bunny83 · Oct 27, 2014 at 01:55 AM
Your code looks extremely strange. In UnityScript every script file is implicitly a class with the name of the file. However you explicitly declare another class in that file which is derived from "overWorld" and called weapon. Weapon derived from overWorld?
Now it gets even stranger. That weapon class is actually empty. You just declared another nested class also called weapon. This class has another nested enum type called "types".
Beside the fact that type-names should start with a capital letter, this whole setup makes not much sense. If you want to use the "types" enum outside of your two nested classes you have to use:
weapon.weapon.types
I have some counter questions to get behind the reason for your strange setup:
What's the filename of the code you posted?
What class is "overWorld"? Is it actually MonoBehaviour or just another custom data-class?
The file name is weapon. overWorld is going to be the parent of weapon.
So for one your saying every script is its own class? so i didnt have to write:
"public class weapon extends overWorld { public class weapon {" ?
I was just doing some more research it seems that i may have to use C#
@Bunny83: Unityscript files are implicitly a class derived from $$anonymous$$onoBehaviour. If you don't want that then you need to explicitly declare a class, like C#. So that part is fine. The wrong stuff is the nested class (there should not be a weapon class inside another weapon class), and the enum should also not be inside the weapon class. Also the correct style is that the class and enum names should be capitalized.
@Eric5h5 I know, but he has code outside his explicit class which usually belongs to the implicit class. I just discovered that the compiler simply merges those two classes as long as the filename and the class name matches. However we don't know the name of the file, so it could actually be another class. That's why i asked those counter questions.
@zolon: What exactly do you mean with parent? You know if you derive a class from a base class the child class inherits everything from the base class. So your structure would actually say a Weapon is also an OverWorld. That just sounds strange to me. Are you sure you've understood what class inheritance is?
edit
Just took a look at the tutorial video. Well, the example in the video is a lot different from what you did here.
Also the turtorial video uses the term Subclass in the wrong context. I know some people call nested classes subclasses but it's actually the wrong term. That's why i usually use child class / derived class, base class and nested class / inner class to avoid that common confusion. The term "parent" is also a bit critical since a parent class has nothing to do with a parent object in Unity. The parent class / base class / super class does not "contain" a child class but the child class is derived from the base class.