Abstract Method Problem

I am new to coding. I have 3 c# script components. CharacterControl.cs is attached to the player game object. Openable.cs is attached to the interactable game object. Interactable.cs is abstract class so i didn’t attach it to anything. I want to call the Interact() method when the player is near the interactable game object. Debug.Log(“rctest”); is fine, it returns “rctest” to console but line 38 rc.transform.GetComponent().Interact(); method should call “public override void Interact()” in Openable class. But it does nothing. Do you guys have any suggestions?
I referenced this video:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour
{
    private GameObject Player;
    private Vector2 boxSize = new Vector2(0.1f, 1f);
    private RaycastHit2D[] hits;

    private void Start()
    {
   Player = GameObject.Find("Player");              
    }
 
   private void Update()
    {       
        if (Input.GetKeyDown(KeyCode.E))
        {
            CheckInteraction();
         
        }
    }

       public void CheckInteraction()
    {

        RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, boxSize, 0, Vector2.zero);

        if (hits.Length > 0)
        {
            foreach(RaycastHit2D rc in hits)
            {
             
                if (rc.transform.GetComponent<Interactable>())
                {
                    Debug.Log("rctest");
                    rc.transform.GetComponent<Interactable>().Interact();
                    return;
                }
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class Openable : Interactable
{
    private GameObject assistant;
    private SpriteRenderer sr;

    public void Awake()
    {
        assistant = GameObject.Find("UI_Assistant");
        sr = GetComponent<SpriteRenderer>();
    }
    public override void Interact()
    {
        assistant.SetActive(true);
        sr.enabled = false;
        Debug.Log("Interactworks");
    }
    public void Start()
    {
        Debug.Log("starttest");
        Interact();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(BoxCollider2D))]
public abstract class Interactable : MonoBehaviour
{
    private void Reset()
    {
        GetComponent<BoxCollider2D>().isTrigger = true;
    }
    public abstract void Interact();

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.GetComponent<CharacterControl>().OpenInteractableIcon();
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.GetComponent<CharacterControl>().CloseInteractableIcon();
    }
}



Unfortunately photographs of code are not a thing.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Beyond that, “doesn’t work” isn’t actually a useful way to describe any problem.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

You may edit your post above.

1 Like

Still not sure what this mean:

But I will observe that I don’t see any code for OpenInteractableIcon() or CloseInteractableIcon()

Have you actually followed the code flow through in your mind, with your finger on the code, line by line? It’s not possible given what you have posted so far, so I suspect you have not.

As for this:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

1 Like

I tested your code, everything works as expected.
The Interact method is execute

public override void Interact()
{
    assistant.SetActive(true);
    sr.enabled = false;
    Debug.Log("Interactworks");
}

and displays a message “Interactworks” in the console.
However, nothing changes on the stage, because the method Interact has already been called at the start.

public void Start()
{
    Debug.Log("starttest");
    Interact();
}
1 Like

I forgot to add “using UnityEngine.UI;” to Openable.cs . My script is a bit diffirent from script in the video so it takes time to notice problem. Thank you for helping me :slight_smile:

So this was just a compiler error!

Compiler errors should take ZERO time to notice because they PREVENT you from playing.

Compiler errors also generally NEVER require posting because you can fix them yourself. Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Make sure your log console selector buttons are enabled. See this graphic:

https://discussions.unity.com/t/733002/10

https://discussions.unity.com/t/804947/4

1 Like

That is weird because i can play the game in unity and Console did not give error about that problem. Just can’t execute “assistant.SetActive(true);” because of library.

What does this mean? ^ ^ ^

forget to add “using UnityEngine.UI”. UnityEngine.UI is library, isn’t it? He said that in the video I watched.