Search Unity

"Object reference not set to an instance of an object"

Discussion in 'Getting Started' started by Mister_Pyxel, Apr 11, 2018.

  1. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    Hi people!

    So, I am trying to make a chess game with a friend, right now I have a Board with 64 cases and I created a script that highlight cases when we fly over with the mouse's cursor.

    The next thing I want to do is higlight brighter the case when I clic the case, but I think I made something wrong, because when I launch the demo and clic a case I get this error :



    I really fell like I made a stupid mistake but since yesterday I didn't managed to find what I did wrong...

    Here is the script I put on the case object :
    Code (CSharp):
    1. //-----Libs-----\\
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. //-----Script-----\\
    8. public class CaseSelection : MonoBehaviour {
    9.  
    10.     //-----Private-----\\
    11.     private Renderer rend;
    12.  
    13.     //-----Public-----\\
    14.     public CaseFocusV2 caseFV2;
    15.  
    16.     //-----Private func-----\\
    17.     private void Start ()
    18.     {
    19.         rend = GetComponent<Renderer>();
    20.     }
    21.  
    22.  
    23.     private void OnMouseDown()
    24.     {
    25.         rend.material.color = caseFV2.Selection(rend);
    26.     }
    27.  

    And here is the script called with "caseFV2.Selection(rend)" (everything until "public func" work well) :
    Code (CSharp):
    1. //-----Libs-----\\
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. //-----Script-----\\
    8. public class CaseFocusV2 : MonoBehaviour
    9. {
    10.  
    11.     //-----Private-----\\
    12.     private int caseMask;
    13.     private float camRayLength = 250f;
    14.     private Boolean caseFocused = false;
    15.     private GameObject previous = null;
    16.     private Color col;
    17.     private Color colSelect;
    18.     private Renderer selectedCase;
    19.     private Color selectedCol;
    20.  
    21.  
    22.     //-----Private func-----\\
    23.     private void Start()
    24.     {
    25.         caseMask = LayerMask.GetMask("Case");
    26.         col.a = 0f;
    27.         selectedCol = Color.white;
    28.     }
    29.  
    30.  
    31.     private void Update()
    32.     {
    33.         CaseSelectionnable();
    34.     }
    35.  
    36.     private void CaseSelectionnable()
    37.     {
    38.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    39.         RaycastHit caseHit;
    40.  
    41.         if (Physics.Raycast(camRay, out caseHit, camRayLength, caseMask))
    42.         {
    43.             if (!caseFocused)
    44.             {
    45.                 col = caseHit.transform.gameObject.GetComponent<Renderer>().material.color;
    46.                 col.a = .4f;
    47.                 caseHit.transform.gameObject.GetComponent<Renderer>().material.color = col;
    48.  
    49.                 previous = caseHit.transform.gameObject;
    50.                 col.a = 0f;
    51.                 caseFocused = true;
    52.             }
    53.             else
    54.             {
    55.                 previous.GetComponent<Renderer>().material.color = col;
    56.                 col = caseHit.transform.gameObject.GetComponent<Renderer>().material.color;
    57.                 col.a = .4f;
    58.                 caseHit.transform.gameObject.GetComponent<Renderer>().material.color = col;
    59.  
    60.                 previous = caseHit.transform.gameObject;
    61.                 col.a = 0f;
    62.             }
    63.         }
    64.     }
    65.     //-----Public func-----\\
    66.     public Color Selection(Renderer selectedRenderer)
    67.     {
    68.         selectedCol = selectedCase.material.color;
    69.         selectedCol.a = 0f;
    70.         selectedCase.material.color = selectedCol;
    71.         colSelect = selectedRenderer.material.color;
    72.         colSelect.a = .6f;
    73.         selectedCase = selectedRenderer;
    74.         return colSelect;
    75.     }
    If you have any question about what the mess I have done with this code ask me :p

    Thanks for your help! :)
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,193
    Have you assigned anything to the caseFV2 field on the inspector for the object the first script is attached to?
     
  3. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    Ho yeah I'm stupid >< indeed I didn't put anything in it... but I tryed to put my caseHolder where I put the script caseFV2 and I didn't accept it, did I need to put the script itself (it seem to not accept it either...)
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You drag in an object (from the Hierarchy tab) that has a CaseFocusV2 script on it.
     
  5. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    I was trying to put the object from the hierarchy in the prefabs script, but I have to do it in the instanciated object, but even once I did that, nothing happend and I get the same error =s
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's very unclear what you are trying to say. Please strive to be more clear and complete when asking for help, so we stand a decent chance of being able to help you.

    A prefab in the project (i.e. Project tab) cannot reference something in the scene (i.e. in the Hierarchy tab). This makes sense: things in the project exist across potentially many different scenes, so how could they reference something in a scene, when that scene might not even be loaded?

    The converse works just fine: an object in any scene can reference a prefab in the project just fine, because everything in the project is always available. It can also reference another object in the same scene (but not, of course, an object in some other scene).

    So when you have a reference to set, drag in something that makes sense, rather than something that doesn't. And of course it needs to be of the correct type (or have a component of the correct type). Then that will work.

    If at that point you're getting an error, then the issue now is something else. Double-click the error, and it will take you right to the line of code that has a problem. You should be able to work out from that what to do. If not, post the exact error here, with all relevant details, and get help. Or paste a vague complaint, and get no help. ;)
     
  7. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    Yes I'm sorry, I even confuse myself, I really don't have the force to explain what I wanted to do, and honestly it was absolutly not the right way to abord the thing.
    I have deleted the "CaseSelection" script, and I just keep the other one, I manage to do waht I wanted (have a visual clue that a case was selected).

    But thank you for trying to help me, if I need help for something else sometime, I will try to be clearer in my post! =)
     
    JoeStrout likes this.