Problem with the Sprite Renderer Color portion

Apologies if the formatting of this post isnt right, this is my first time posting (Any Tips Welcome!)

I am getting the following error message;

NullReferenceException: Object reference not set to an instance of an object
Delivery.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Delivery.cs:31)

Below is my code;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Delivery : MonoBehaviour

{

[SerializeField] Color32 hasPackageColor = new Color32 (1, 1, 1, 1);

[SerializeField] Color32 noPackageColor = new Color32 (1, 1, 1, 1);

[SerializeField] float destroyDelay = 0.5f;

bool hasPackage;

SpriteRenderer spriteRenderer;

void start ()

{

    spriteRenderer = GetComponent<SpriteRenderer>();

}

void OnCollisionEnter2D(Collision2D other)

{

    Debug.Log("Smash!!");

}



void OnTriggerEnter2D(Collider2D other)

{

    if (other.tag == "Package" && !hasPackage)

    {

        Debug.Log ("Package Collected!");

        hasPackage = true;

        spriteRenderer.color = hasPackageColor;

        Destroy (other.gameObject, destroyDelay);

    }

       

    if (other.tag == "Customer" && hasPackage)

    {

        Debug.Log ("Package Delivered!");

        hasPackage = false;

        spriteRenderer.color = noPackageColor;

    }

}

}

Can anyone help with this?

Many Thanks.

Welcome to the community!

Look at your start function, try changing start to Start which means that it has a capital letter, I assume the error is because you can’t access sprite renderer in the way your trying to without having a proper start function, therefore all the times you try to access sprite renderer you can’t

What you need to do: change start to Start, make it capital

Hope this helps!

The joys of coding!

Changing one letter to an uppercase solved the problem! :grinning:

Thanks so much!

There sure are a lot of joys to coding, glad I could help you out with your problem!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms