C# Inharetence Problem - GameManager is not updating

Hello, i am making a Halo remake, and am working on the plasma grenade.

Intended function:
The player walks through a trigger(a floating, glowing, grenade) to set plasmaGernadeAmmo to True. This bool is stored in the gameManager, and referenced by the player object when ThrowGrenade is called.

Problem:
The player walks through the trigger, and the trigger reports using Debug.log that it changed the plasmaGernadeAmmo to true. The GameManager and Player object still reports using debug.log that its not true.

here is my three cripts. #1 is the trigger #2 is the GameManager #3 is the player object.

using UnityEngine;
using System.Collections;

public class GernadePickup : PlayerManager {

    public void OnTriggerEnter(Collider other){
        Debug.Log ("Plasma gernade touched. plasmaGernadeAmmo = " + plasmaGernadeAmmo);
            plasmaGernadeAmmo = true;
        Debug.Log ("UPDATE - plasmaGernadeAmmo = " + plasmaGernadeAmmo);
    }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}
using UnityEngine;
using System.Collections;

public class PlayerManager : MonoBehaviour {
    //this script will be used to determan if the player has ammo.

    public bool plasmaGernadeAmmo;


    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        Debug.Log("PLayer Manager - plasmaGernadeAmmo = " + plasmaGernadeAmmo);
    }
}
using UnityEngine;
using System.Collections;

public class GernadeThrow : PlayerManager {
    public GameObject gernadePrefab;
    private Camera mainCamera;
    public bool hasGernade;
    public float throwFource;
    // Use this for initialization
    void Start () {
        mainCamera = Camera.main;
   
    }
   
    // Update is called once per frame
    public void Update () {
        Debug.Log ("plasmaGernadeAmmo = " + plasmaGernadeAmmo);
        if(Input.GetButtonDown ("Fire1")){
            Debug.Log ("Gernade Thrown Requested");
            if(plasmaGernadeAmmo == true){
                Debug.Log ("Plasma gernade Thrown");
                ThrowGernade();
            }
        }
   
    }
    void ThrowGernade(){
        Vector3 vec = mainCamera.ViewportToWorldPoint(new Vector3(0.5F, 0.5F, 0));
        GameObject Gernade = (GameObject)Instantiate(gernadePrefab, vec, mainCamera.transform.rotation);
        Gernade.rigidbody.AddForce(Gernade.transform.forward * throwFource);
        //Gernade.rigidbody.velocity = transform.forward * throwFource;

    }
}

I’m sorry for it being sloppy, I wanted to give it to you in its raw form.

If anyone has recommendations on how to do this in a better way, please tell ^.^

The child will always take precedent when it comes to inheritence. For methods you want to have for both the BaseClass and ChildClass you need to mark them as virtual. Then when you inherit you will use the override keyword* and make sure you call base.MethodName() so the parent can also execute the method as well.

So i cant change the value of a variable in the parent class, from the child class, without using a function?

You have two different classes that inherit from one base class. Which one do you have attached to the object? I’m thinking inheritance isn’t what you think it is- if you have all three of these attached to one object, for instance, there’s three different instances of “PlayerManager” objects. Changing the value of a bool on one is not going to change it for the other two, because they’re different objects that happen to share the same inherited structure. That means that all three would have their own “public bool plasmaGernadeAmmo;” field.

You can absolutely change the value of a variable in the parent class from the child class without a function, no effort at all, but it’s not going to change the value in different objects. Think of components like backpacks- you can inherit from a class that says the backpack needs to have 3 different zipper compartments, and you can inherit that on multiple types of components, but all that changes is that they all have 3 different zippered compartments in their structure- they aren’t all using the SAME 3 compartments.

If you want to have a script that holds the grenade bool and a completely different script to throw that grenade if it exists, then just cut out that inheritance stuff and use GetComponent().plasmaGrenadeAmmo to access that field from any other component attached to the same GameObject.

2 Likes

That explains it, the three scripts are on three different game objects.

With that in mind i cut out the failed inharetence coding, and used other.GetComponent<GernadeThrow>().plasmaGernadeAmmo = true; This worked just fine, I wasn’t sure if this was the best way to do it when i wanted to work on the GUI. I was trying to think ahead by using the playermanager.