Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in '2D' started by lightinside, Jun 5, 2016.

  1. lightinside

    lightinside

    Joined:
    Jun 5, 2016
    Posts:
    1
    Hi,
    I want get color of pixel from sprite. It's my code. What can I do?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.    Texture2D b;
    7.     Sprite bin;
    8.     void Start () {
    9.  
    10.         bin = Resources.Load("kosz", typeof(Sprite)) as Sprite;
    11.         b = bin.texture;
    12.         Color color = b.GetPixel(1, 1);
    13.         Debug.Log(color);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.     }
    19. }
    I have error which reports me my variable is empty and I don't know why. My sprite I have in dir called "picture". I already tried "picture/kosz","picture/kosz.jpg", "kosz", "kosz.jpg". What I do wrong?

    This is my error:
     
  2. manelizzard

    manelizzard

    Joined:
    Feb 4, 2015
    Posts:
    40
    Is the "bin" Sprite linked to a GameObject in the editor? If not, try doing:

    Code (CSharp):
    1. bin = Instantiate(Resources.Load("kosz", typeof(Sprite))) as Sprite;
     
  3. sliverkf

    sliverkf

    Joined:
    Mar 10, 2014
    Posts:
    6
    Hi,

    the sprite HAVE to be in a folder called: Resources