0
\$\begingroup\$
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightsEffect : MonoBehaviour
{
    public List<UnityEngine.GameObject> waypoints;
    public int howmanylight = 5;
    public Generatenumbers gn;
    public bool changeLightsDirection = false;
    public float delay = 0.1f;

    private List<UnityEngine.GameObject> objects;
    private Renderer[] renderers;
    private int greenIndex = 0;
    private float lastChangeTime;

    private void Start()
    {
        //waypoints = new List<UnityEngine.GameObject>();
        objects = new List<UnityEngine.GameObject>();

        if (howmanylight > 0)
        {
            UnityEngine.GameObject go1 = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Sphere);
            duplicateObject(go1, howmanylight);
            LightsEffects();
        }
    }

    private void Update()
    {
        LightsEffectCore();
    }

    public void duplicateObject(UnityEngine.GameObject original, int howmany)
    {
        howmany++;
        for (int i = 0; i < waypoints.Count - 1; i++)
        {
            for (int j = 1; j < howmany; j++)
            {
                Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
                UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0, position.z), Quaternion.identity);
                go.transform.localScale = new Vector3(0.3f, 0.1f, 0.3f);
                objects.Add(go);
            }
        }
    }

    private void LightsEffects()
    {
        renderers = new Renderer[objects.Count];
        for (int i = 0; i < renderers.Length; i++)
        {
            renderers[i] = objects[i].GetComponent<Renderer>();
            renderers[i].material.color = Color.red;
        }

        // Set green color to the first one
        greenIndex = 0;
        renderers[greenIndex].material.color = Color.green;
    }

    private void LightsEffectCore()
    {
        // Change color each `delay` seconds
        if (Time.time > lastChangeTime + delay)
        {
            lastChangeTime = Time.time;

            // Set color of the last renderer to red
            // and the color of the current one to green
            renderers[greenIndex].material.color = Color.red;
            if (changeLightsDirection == true)
            {
                Array.Reverse(renderers);
                changeLightsDirection = false;
            }
            greenIndex = (greenIndex + 1) % renderers.Length;
            renderers[greenIndex].material.color = Color.green;
        }
    }
}

In this line if for example objects count is 5 then there will be 5 renderers but they will be all null.

renderers = new Renderer[objects.Count];
\$\endgroup\$
2
\$\begingroup\$

Because Renderer is a reference type.

In C#, creating an array of any reference type initializes all entries in that array to null. To give these newly-created entries other values, you have to do so explicitly.

Note that you appear to be trying to do so in the code that follows immediately after:

for (int i = 0; i < renderers.Length; i++)
{
    renderers[i] = objects[i].GetComponent<Renderer>();
    renderers[i].material.color = Color.red;
}

On the surface, this is correct and should result in renderers containing five non-null references assuming GetComponent is not itself returning null. If it is, your attempt to access renderers[i].material is going to throw an exception.

If that's the ultimate behavior you are seeing, you probably have a bug in that objects[i] doesn't have a Renderer component. It's probably not set up correctly or is not the object you expected, for example.

If you're not seeing a NullReferenceException, and you're just looking at renderers in the debugger immediately after the new but before you've executed that for loop, then everything is as it should be.

\$\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.