Guys, in connection with the situation I had to get acquainted with inheritance on a quick basis, I understood the principle, but in fact the trouble, when I added grade 1, everything was normal, when I started writing grade 2, and take variables out of 1. He simply does not see them, but this is what, when I save the script, try to start the scene, the unit swears, you forgot to correct the mistakes, but the console is silent, and monodevelop is also silent.

I was told that there are problems with inheritance, but where? I really need your help, all hope for you. The code seems to be simple, understandable, if you write something, I will designate what works. What needs to be fixed? How to return console messages?

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Weapon : MonoBehaviour { class BaseWeapon { public int weapon_damage, ammo_amount, reload_time; public bool weapon_active=false; public GameObject pistolprefab; public GameObject autorifleprefab; public GameObject currect_weapon; public GameObject previos_weapon; public BaseWeapon pistol = new BaseWeapon(10, 7, 2); public BaseWeapon autorifle = new BaseWeapon(20, 30, 5); public BaseWeapon(int wd, int aa, int rt) { weapon_damage = wd; ammo_amount = aa; reload_time = rt; } public void weapon_on_off() { if (weapon_active=true) { currect_weapon.SetActive(true); previos_weapon.SetActive(false); } } } class ManagerWeapon { public void change_weapon() { if (Input.GetKey("1")) { currect_weapon=pistol; previos_weapon=autorifle; weapon_active=true; } if (Input.GetKey("2")) { currect_weapon=autorifle; previos_weapon=pistol; weapon_active=true; } } } void Start() { currect_weapon=pistol; previos_weapon=autorifle; weapon_active=true; } } 
  • one
    and where is your inheritance (except Weapon: MonoBehaviour )? You have some hellish porridge from the nested classes. It probably makes sense for you to learn the very basics of the language and OOP - DreamChild

2 answers 2

As far as I remember, C # allows only one class in one file. And the Weapon class, as I understand it, is useless. You should have the following structure.

BaseWeapon.cpp file:

 public class BaseWeapon : MonoBehaviour { ... } 

ManagerWeapon.cpp file:

 public class ManagerWeapon : BaseWeapon{ ... } 

Those. BaseWeapon inherits MonoBehaviour, and ManagerWeapon inherits BaseWeapon and MonoBehaviour too, as BaseWeapon inherits from it :)

  • one
    In C #, it is allowed to define several classes in one file. Source code files usually have a .cs extension. - AlexeyM 5:09

Colleague you have no inheritance here. As my consultant said: "Simultaneously, we cook borscht and dumplings in one pan." Variables

 public GameObject pistolprefab; public GameObject autorifleprefab; public GameObject currect_weapon; public GameObject previos_weapon; public BaseWeapon pistol = new BaseWeapon(10, 7, 2); public BaseWeapon autorifle = new BaseWeapon(20, 30, 5); 

should be moved to the body of the WeaponManager . Because its task, if I understand correctly, is to create an arsenal of weapons and switch between different types. The weapon itself should be aware of which types it should not exist, and it is not at all logical that it creates itself. So I would inherit WeaponManager from MonoBehaviour and transfer Start and weapon_on_off Perhaps you wanted to make BaseWeapon base weapon BaseWeapon , and from it already inherit other weapons like machine gun and pistol.

 public class BaseWeapon : MonoBehaviour { public int weapon_damage, ammo_amount, reload_time; public bool weapon_active=false; public BaseWeapon(int wd, int aa, int rt) { weapon_damage = wd; ammo_amount = aa; reload_time = rt; } ... } 

MonoBehaviour specify if you want to hang the weapon as a component on objects. If for you this is a data warehouse, then you can not inherit from MonoBehaviour . And already your classes heirs will look like this:

Pistol:

 public class Pistole: BaseWeapon { ... } 

Rifle:

 public class Riffle: BaseWeapon { ... } 

In each of the descendants of Pistole and Riffle , any public variable defined in the parent class will be available. Fedor, my advice to you, divide the classes according to different files of the same name, this will help you in the future not to be confused when you need to find the required class.