0
\$\begingroup\$

I asked about this question How to efficiently implement a 7-segment display? in previous post. Now I just add an update function to the last part of the code to display the time(I don't bother the exact time at the moment just want to make sure the number change regularly). However,I'm not sure why the update only run once, I checked the answer in this website, mentioned the root cause is gameobject deactivated(Update function only running once) but this is not my case. I tried with void OnMouseDown(){Display(DateTime.Now.Second)}, the number does change according to my mouse button so I'm not sure what wrong with Update function. In addition, I assigned all the variable but it still pop out message unassigned reference exception, not sure where the problem.

using System;
using UnityEngine;

public class ClockDigit : MonoBehaviour
{
//  Assuming you number your segments as follows:
//   -- 0 --
//  |       |
//  5       1
//  |       |
//   -- 6 --
//  |       |
//  4       2
//  |       |
//   -- 3 --
// Store a lookup table for which segments 
// should be active when displaying each digit.
static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {

    {true,  true,  true,  true,  true,  true,  false}, // 0
    {false, true,  true,  false, false, false, false}, // 1
    {true,  true,  false, true,  true,  false, true }, // 2
    {true,  true,  true,  true,  false, false, true }, // 3
    {false, true,  true,  false, false, true,  true }, // 4
    {true,  false, true,  true,  false, true,  true }, // 5
    {true,  false, true,  true,  true,  true,  true }, // 6
    {true,  true,  true,  false, false, false, false}, // 7
    {true,  true,  true,  true,  true,  true,  true }, // 8
    {true,  true,  true,  true,  false, true,  true }  // 9
};

public Color32 activeColour = Color.red;
public Color32 inactiveColour = Color.black;
public SpriteRenderer[] segments = new SpriteRenderer[7];    

public void Display(int number)
{
    var digit = number % 10;
    if (digit < 0) digit *= -1;
    for (int i = 0; i < 7; i++)
    {
        if (SEGMENT_IS_ACTIVE[digit, i])
        {
            segments[i].color = activeColour;
        }
        else
        {
            segments[i].color = inactiveColour;
        }
    }
}

public void Update()
{
    Display(DateTime.Now.Second);
           
}
}

I tried this command also, the infinite loop freeze the Unity and FixedUpdate() command also update the frame only one time.

void Update()
    {while(true)
      Display(DateTime.Now.Second);
    }

enter image description here

\$\endgroup\$
9
  • 3
    \$\begingroup\$ See the red error message at the bottom of your screen? You have an unassigned reference exception. Looks like you forgot to assign some of your references in the Inspector. I'm surprised to see this script on one of the individual rectangles though — you don't have 7 copies of this script in your scene, one on every segment, do you? You only need one copy for it to do its job. \$\endgroup\$ – DMGregory Jan 14 at 13:09
  • \$\begingroup\$ The script itself runs without error. Make an empty gameobject for your counter that just contains the script and remove all other attached scripts of ClockDigit \$\endgroup\$ – Zibelas Jan 14 at 13:22
  • \$\begingroup\$ Yeah I understood what the error suppose to mean but I don't spot any more reference I can assign in inspector. The is the part I confuse, I created 7 object(through"create empty") in unity, assign the script to one of the rectangle(Rect1) only, and from the script,assign the 7 spriterenderer to respective gameobject. If this is not correct, then how is it done? \$\endgroup\$ – chuackt Jan 14 at 13:59
  • 1
    \$\begingroup\$ Just click on the red message in your editor and it will tell you where else you placed your script since it brings you to the error message \$\endgroup\$ – Zibelas Jan 14 at 14:26
  • 1
    \$\begingroup\$ @chuackt There's a minimum wait time for accepting your own answer to give the community adequate time to answer. Accepting an answer is different than closing a question. \$\endgroup\$ – Pikalek Jan 23 at 16:58
1
\$\begingroup\$

I found the problem already. I made a silly mistake by accidently added the script to main camera. When the Update function run, it encountered problem and stopped, so it only run once. Commenting out the Display(DateTime.Now.Second)and replace with a debug log, the debug log run indefinitely as no error encountered. I remove the script from main camera and it working as intended.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.