Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Project Files

A topic by Booper created May 11, 2021 Views: 123 Replies: 4
Viewing posts 1 to 4

Thanks for hosting this jam! Any chance you could share the .cs files from the tutorial? Or if you could point me in the right direction, it would be much appreciated. I'm getting a null reference exception in Interactor for:

if (interactImage.sprite != defaultIcon)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Interactor : MonoBehaviour
{
    public LayerMask interactableLayermask = 8;
    public Interactable interactable;
    public Image interactImage;
    public Sprite defaultIcon;
    public Vector2 defaultIconSize;
    public Sprite defaultInteractIcon;
    public Vector2 defaultInteractIconSize;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2, interactableLayermask))
        {
            if (hit.collider.GetComponent<Interactable>() != false)
            {
                if (interactable == null || interactable.ID != hit.collider.GetComponent<Interactable>().ID)
                {
                    interactable = hit.collider.GetComponent<Interactable>();
                }
                if (interactable.interactIcon != null)
                {
                    interactImage.sprite = interactable.interactIcon;
                    if (interactable.iconSize == Vector2.zero)
                    {
                        interactImage.rectTransform.sizeDelta = defaultInteractIconSize;
                    }
                    else
                    {
                        interactImage.rectTransform.sizeDelta = interactable.iconSize;
                    }
                }
                else
                {
                    interactImage.sprite = defaultInteractIcon;
                    interactImage.rectTransform.sizeDelta = defaultInteractIconSize;
                }
                if (Input.GetKeyDown(KeyCode.E))
                {
                    interactable.onInteract.Invoke();
                }
            }
        }
            else
            {
                if (interactImage.sprite != defaultIcon)
                {
                    interactImage.sprite = defaultIcon;
                    interactImage.rectTransform.sizeDelta = defaultIconSize;
                }
            }
    }
}

Oops, I wasn't creating the Canvas. I'll leave this up in case anyone else missed this! Thanks!

Host

Glad you figured it out!  Excited to see what you make!

(1 edit)

Do I need to use exactly the same code or can I implement logic like this?
I have used code like this in previous projects and I know how it works, however, if use this code is a prerequisite, I will use it.

Host

Sorry I didn't see your comment.  It needs to use the tutorial as a base so add to it as much as you like but careful what you take away.  You'll probably be fine if you have a raycast interact system where the interactable is where you store the data and the interactor is where you have functionality.  It's best to just use the tutorial.