Trouble with inheritance

Hi guys. I am having a really annoying problem with inheritance.
I am using UI to dynamically populate a shop with Weapons from a list.

Weapon is the base class and I then have other classes such as Pistol, Machinegun, etc, that extend Weapon.

public abstract class Weapon : MonoBehaviour{
	public int weaponID;
}

public class Pistol : Weapon {
public Pistol() {
	weaponID = 1; // test value;
}

public class MachineGun : Weapon {
public MachineGun () {
	weaponID = 2; // test value;
}

for (int i = 0; i < weaponsForSale.Length; i++) {
			GameObject weaponPrefab = Instantiate (weaponsForSale *);*
  •  	Weapon weaponComp = weaponPrefab.GetComponent<Weapon>();*
    
  •  	Debug.Log(weaponComp); // this displays the correct class, Pistol, Machinegun, etc*
    
  •  	Debug.Log(weaponComp.weaponID); // this displays 0, the base stat of weapon instead of the pistol, machinegun, etc*
    

Well, if your editor hardware does support compute shaders but they simply doesn't work when the target is Android you should file [a bug report as suggested here.][1]. You may have a look at [the bottom of this page][2]. It might be possible that OpenGL ES 3.1 might not be supported by your PC hardware. What OS and hardware do you use? [1]: https://blogs.unity3d.com/2015/05/26/dx11-features-on-mobile/ [2]: https://docs.unity3d.com/Manual/OpenGLCoreDetails.html

2 Answers

2

You cannot/shouldn’t call a constructor in Unity like that. Instead call Awake, Start, etc.

public class Pistol : Weapon {
 void Start() {
     weaponID = 1; // test value;
 }
}

Don't you need to override values set in base classes? Don't they have to be virtual?

I'm using Windows 10 and I have a GTX 1070. I guess I might not have support for OpenGL ES 3. Do you know if there's a way to force unity to use other rendering methods (the one I'm using when Windows is my target platform) while having Android as a selected Platform ? Otherwise I can't work while I have Android as a selected platform :(

@spraw I was using Start to no avail, problem persists.

Edit: Awake seems to be working, but not Start o.o
Thank you.

Hi, I've run into same issues. If your are still looking for some solution it is quite easy. In Unity Editor there is a thing called Graphics Emulator and I guess it will be set to OpenGL ES 3.0 in your case. But OpenGL ES 3.0 has not support for compute shaders, it's present since API 21 with OpenGL 3.1. Just turn off Graphics Emulation and you are ready to go ;)