Hi I’m trying to find a better way to sequence functions in script.
essentially I have my mainCharacter going across the screen when he triggers the scene end the scene is de-activated and the next scene becomes active. (also the mainCharacter resets to the beginning) It requires me to have a list of if statements. How would I do this in a better way? Arrays? enums maybe? Or should I use events? Thanks for your input.
if (mainCharacter.transform.position.x > scene.end) {sceneNum = sceneNum +1;}
if (sceneNum == 1) scene.one.SetActive(true); else scene.one.SetActive(false);
if (sceneNum == 2) scene.two.SetActive(true); else scene.two.SetActive(false);
if (sceneNum == 3) scene.three.SetActive(true); else scene.three.SetActive(false);
if (sceneNum == 4) scene.four.SetActive(true); else scene.four.SetActive(false);
if (sceneNum == 5) scene.five.SetActive(true); else scene.five.SetActive(false);
What do you mean? To call a function stored inside collection?
using UnityEngine;
using System.Collections.Generic;
using System;
public class SequencedFunctionCaller : MonoBehaviour {
private List<Action> actions = new List<Action>(); // Action is a callable type without arguments
// Use this for initialization
void Start () {
actions.Add(() => Debug.Log("hello world")); // lambda function
actions.Add(someFunction);
actions[1](); // calls someFunction
call (1); // same
}
void someFunction() {
Debug.Log("some function");
}
void call(int i) {
actions[i]();
}
// Update is called once per frame
void Update () {
}
}
And please look at UnityEvent:
It can be added as public field into MonoBehaviour and it has nice GUI. You can store UnityEvents into List too. And invoke them with event_.Invoke()_call;
A switch may be the better choice here as I need to add custom functions everytime the scene changes. I want to figure out how events work but I’m using js and the docs and examples are fairly confusing or just broken.
Perhaps next project I’ll switch to C#. I’m still just a 6 months noob at scripting.
#pragma strict
import UnityEngine.Events;
var events : UnityEvent[];
// this code will invoke all events on Start which you can specify by Inspector
// just put the component on some gameobject and add some events
function Start () {
for (var i = 0; i < events.Length; i++) {
events[i].Invoke();
}
}
One last thing, Let say the main character.position.x is greater than scene.position.x. When its true i invoke an event. I check for this in an if statement in update. When it becomes true it sends and event just once until it resets. Whats the best way to do this? I keep reading to use co-routine but haven’t found an example of how it would work.
I usually use just simple boolean flag for such task.
#pragma strict
import UnityEngine.Events;
var Character : Transform;
var Scene : Transform;
var OnPositionEvent : UnityEvent;
private var eventFired : boolean;
function Start () {
}
function Update () {
if (!eventFired && Character.position.x > Scene.position.x) {
OnPositionEvent.Invoke();
eventFired = true;
}
}
function Reset() {
eventFired = false;
}
function OnGUI() {
if (GUILayout.Button("Reset")) {
Reset();
}
}
I don’t know how to use coroutine for this. If you can point me into some link you read about it I will be thankful.
I just googled “call function once from update” there are a lot of postings but a boolean flag seems to be the only way. Others keep saying to use co-routine without giving an example. There must be a simpler and more elegant way to do this. I guess this isn’t terrible.