Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Unity C Coding Help Needed Getting my lights to work

A topic by Cas Windsor created May 25, 2020 Views: 130 Replies: 6
Viewing posts 1 to 7
Submitted

For hours now I have been running in circles with the code for my lights and would appreciate some help.

Basically I have four "T I M E" lights at the top of my playfield.  When all of them are lit, a bonus multiplier goes up.

What I need is a way to get the lights which are lit or dimmed to shift left or right when the flippers are pressed.

Submitted

Submitted (1 edit)

So they all start out in the off state then light up as the ball rolls over the switches below them.  I have two scripts, one called Rollover.

This is attached to one of the four triggers and animates the switch, lights or dims the light above it, sets a lightOn boolean and passes this information to another script called TimeLightsFeature...

-----------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RollOver : MonoBehaviour
{
    [SerializeField] public Animator rolloverAnimator;
    public Material[] lightMaterials;
    public Renderer lightRenderer;
    public bool lightOn = false;
    public int thisLightNumber;

    private TimeLightsFeature timeLightsFeature;

    void Awake() 
    {
        timeLightsFeature = GameObject.FindObjectOfType<TimeLightsFeature>();
    }

    void Start()
    {
        lightRenderer.enabled = true;
        if (lightOn == false)
        {
            lightRenderer.sharedMaterial = lightMaterials[0];
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        rolloverAnimator.SetBool("RolloverDown",true);

        if (lightOn == false)
        {
            lightRenderer.sharedMaterial = lightMaterials[1];
            lightOn = true;
            timeLightsFeature.UpdateLightArray(thisLightNumber, true);
            return;
        }
        else if (lightOn == true)
        {
            lightRenderer.sharedMaterial = lightMaterials[0];
            lightOn = false;
            timeLightsFeature.UpdateLightArray(thisLightNumber, false);
            return;
        }

    }

    private void OnTriggerExit(Collider other)
    {
        rolloverAnimator.SetBool("RolloverDown", false);
    }
}

-------------------

 TimeLightsFeature currently only has this in it...

---------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeLightsFeature : MonoBehaviour
{
    bool allTrue = false;
    [SerializeField] bool[] isTimeLightLit;
    
    public void UpdateLightArray(int lightNumber, bool isLit)
    {
        isTimeLightLit[lightNumber] = isLit;
    }
}

----------------------

I'm not sure the best way to structure this or even if I should be using lists instead of arrays to get the booleans to shift left and right.

Any help would be appreciated.  

HostSubmitted

Share this in the #unity channel of the Discord: https://discord.com/invite/eUSFZdJ
I'm sure someone there will be able to help you :)

Submitted

I would if I could but unfortunately I’ve been unable to get into Discord.  This invite keeps coming up as invalid and each time I try to log in I get the verification by phone message follow by… something went wrong.

I’ve also messaged Discord but they’ve only sent me automated replies with no help.

As for my code, I think I just need to separate and organise my scripts better then experiment with lists.   I'll have another go tomorrow.

Submitted

OK, I'm into Discord.  Had to install the app instead of going through my browser.  Will have a better look tomorrow.  Thanks.

Submitted

I'm in the middle of cooking dinner but at a high level can you just keep the lights in an array and use a for loop to work through them when a flipper event is triggered?

eg 

float light[whatever]

float newlight[whatever];

for i = 0; i < whatever; i++

{

if light[i] == true then newlight [i+1] = true


then copy newlight[] into light[]