Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change material color with script

Discussion in 'Scripting' started by getmyisland_dev, Jul 8, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    I have 2 scripts that are trying to change the material color.

    The first script is attached to my canvas where I have some UI buttons that should change my material color when I press them.
    Here is the code:
    Code (CSharp):
    1. public class CharacterCustomization : MonoBehaviour
    2. {
    3.     [SerializeField] Color[] allColors;
    4.  
    5.     public void SetColor(int colorIndex)
    6.     {
    7.         PlayerManager.localPlayer.SetColor(allColors[colorIndex]);
    8.     }
    9. }
    This script then reaches out to the player manager and the player manager changes the material color. (Thats what it should do at least).

    Here is the player manager script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour
    6. {
    7.     bool hasControl;
    8.     public static PlayerManager localPlayer;
    9.  
    10.     //Player Color
    11.     static Color myColor;
    12.  
    13.     [SerializeField] Renderer renderer;
    14.  
    15.     void Start()
    16.     {
    17.         renderer = GetComponent<Renderer>();
    18.  
    19.         if (hasControl)
    20.         {
    21.             localPlayer = this;
    22.         }
    23.  
    24.         if (myColor == Color.clear)
    25.             myColor = Color.white;
    26.         renderer.material.color = myColor;
    27.     }
    28.  
    29.     public void SetColor(Color newColor)
    30.     {
    31.         Debug.Log("Set Color");
    32.  
    33.         myColor = newColor;
    34.         if (renderer != null)
    35.         {
    36.             renderer.material.color = myColor;
    37.         }
    38.     }
    39. }
    This is the error it throws out:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. CharacterCustomization.SetColor (System.Int32 colorIndex) (at Assets/Scripts/Interact/CharacterCustomization.cs:11)
    The error says that something is null. In my case I assume its the color index.

    I know that this is a lot of code, but I would really appreciate it if someone can overlook my code and can tell me if something is wrong with the code.

    Thanks in advance Max.
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    200
    Well if it says that PlayerManager.localPlayer is null maybe you make sure it is not null
     
    getmyisland_dev likes this.
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    And again you helped me. Looks like I messed something up and I overlooked it all the time. Thank you so much.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Remember, what you are doing is COMPLETELY IRRELEVANT to this error.

    Why? As I have posted so many times, it is because the answer is always the same... ALWAYS.

    It is the single most common error ever... and you must fix it FIRST, before any other fixes.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

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

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    khrysller likes this.