Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Public override void

Discussion in 'Scripting' started by paxorus68, Oct 15, 2020.

  1. paxorus68

    paxorus68

    Joined:
    Oct 11, 2020
    Posts:
    2
    1r Program:

    using UnityEngine;
    public class PickUpObjects : MonoBehaviour
    {
    public GameObject ObjectToPickUp;
    public GameObject PickedObject;

    private void Update()
    {
    if (ObjectToPickUp != null && ObjectToPickUp.GetComponentInParent<PickableObject>().isPickable == true && PickedObject == null)
    {
    if (Input.GetKeyDown(KeyCode.E))
    {
    PickedObject = ObjectToPickUp;
    PickedObject.GetComponent<PickableObject>().isPickable = false;
    Interact();
    }
    }

    }

    public virtual void Interact()
    {
    Debug.Log("j");
    }
    }

    2n Program:

    using UnityEngine;

    public class PickableObject : PickUpObjects
    {
    public Item item;
    public bool isPickable = true;

    public override void Interact()
    {
    Debug.Log("s");
    base.Interact();
    }


    private void OnTriggerEnter(Collider other)
    {
    if (other.tag == "PlayerInteractionZone")
    {
    other.GetComponentInParent<PickUpObjects>().ObjectToPickUp = this.gameObject;
    }
    }

    private void OnTriggerExit(Collider other)
    {
    if (other.tag == "PlayerInteractionZone")
    {
    other.GetComponentInParent<PickUpObjects>().ObjectToPickUp = null;
    }

    }
    }

    The public override void doesn't works. The letter "J" in the first program comes out on the console of Unity, but the letter "S" not.
    Anyone can tell me why? Thanks.
     
  2. bixarrio

    bixarrio

    Joined:
    Dec 29, 2017
    Posts:
    18
    Please use the code tags for code.

    I have run your code and it works fine. Are you sure you attached the correct script to the object?
     
    Last edited: Oct 15, 2020
    Bunny83 and PraetorBlue like this.
  3. paxorus68

    paxorus68

    Joined:
    Oct 11, 2020
    Posts:
    2
    Thanks for the help, i will write all the code another time.
    And yes, this code is in the right object.