How Can I Run C# Script Through Unity Event
I have a game object called LightingPole with a Spotlight object attached. I am trying to turn all instances of the Spotlight object on at an event action called "On Night Actions" and all instances of Spotlight object off at "On Day Actions".
To do this I have created/discovered a script as below: (I'm a complete beginner with coding)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsNight : MonoBehaviour
{
private GameObject Spotlight;
private GameObject[] StreetLights;
void Start()
{
StreetLights = GameObject.FindGameObjectsWithTag("StreetLightsTag");
foreach (GameObject Spotlight in StreetLights)
{
// Set Spotlight game object <Light> to active
Spotlight.GetComponent<Light>().enabled = true;
}
}
}
I am just not sure that my script is correct in order to search for all instances of the Spotlight TAG and then activate (or decativate) the Light attached to the object.
I have created two versions one that enables the light and one that disables it/them.
I am just not sure what I need to input into the Action Boxes
Especially the MonoScript.name filed. I believe I drop the scripts into the RunTime Only field.
But that's as far as I can get with this and getting the lights to turn on and off. Any help much appreciated but remember I don't know a lot about C# as yet. Many thanks.
Answer by tedesignz1 · Apr 20, 2018 at 03:49 PM
You really only need one script. You can call it LightManager.cs
In that script you'll need two methods:
public void LightsNight()
{
// turn all lights on here
}
public void LightsDay()
{
// turn all lights off here
}
Then those methods will automatically show up in the drop-down list. Select the method and that code will execute when the event is triggered.
Answer by Hawk0077 · Apr 20, 2018 at 04:03 PM
Thanks for the response @redesignz1 and the hint but I really don't know enough to create that script and then push it through the Action Box.
Looks like I am going to have to go to freelancer.com for this one.
I recommend posting on Unity Connect at connect.unity.com. You're probably more likely to get a qualified developer there. I just joined a few days ago, and it just so happens I'm looking for freelance work. You can check out my portfolio here: https://connect.unity.com/u/59c8c31f32b306002794f318
Thanks, I will probably wait to learn the code myself as most developers I find in the past are way overpriced for my budget in general. Someone just actually quoted me $400 which is kind of a joke because I know it would only take 10 $$anonymous$$utes for a qualified developer to complete. But thanks.