I am creating a channel-based object movement script (designed for doors) and everything was working before I decided to add both MoveTowards and Lerp modes to it using an Enum.
Once I did this, I lost the ability to check for objects on that channel (line 20).
using UnityEngine;
using System.Collections;
public class GateScript : MonoBehaviour
{
public bool IsActive = true;
public Vector3 Movement;
public int BroadcastChannel;
public enum Method { MoveTowards, Lerp };
public Method MovementMethod = Method.MoveTowards;
private GateControl[] ControlsPanels;
void Start()
{
GateControl[] ControlsPanels = FindObjectsOfType(typeof(GateControl)) as GateControl[];
}
void Update()
{
foreach (GateControl ControlPanel in ControlsPanels)
{
if (ControlPanel.BroadcastChannel == BroadcastChannel && ControlPanel.SignalSent)
{
this.gameObject.transform.position += Movement;
if (MovementMethod == Method.MoveTowards)
{
this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, Movement, Time.deltaTime);
}
if (MovementMethod == Method.Lerp)
{
this.gameObject.transform.position = Vector3.Lerp(this.gameObject.transform.position, Movement, Time.deltaTime);
}
}
}
}
}
I can’t see anything that would cause an error - can you give more details on what “I lost the ability to check for objects” means? And what “No reference” is? Any errors output to the console?
As an aside, I’d also use a switch statement rather than if statements for the enum value checks (but this is mostly a style thing and will not cause any errors).
EDIT: just noticed your error, in Start() you create a local version of GateControl[ ] ControlsPanels, remove the GateControl[ ] from that.
From what I can see, this is just another method of, it is no better or more efficient, it still uses essentially the same premise. But this still does not fix my infinite movement problem.
I need to be able to check for SignalSent constantly, but by running the MoveTowards or Lerp every frame, the object constantly moves without stopping (obviously).
using UnityEngine;
using System.Collections;
public class GateScript : MonoBehaviour
{
public bool IsActive = true;
public Vector3 Movement;
public int BroadcastChannel;
public enum Method { MoveTowards, Lerp };
public Method MovementMethod = Method.MoveTowards;
private GateControl[] ControlsPanels;
void Start()
{
ControlsPanels = FindObjectsOfType(typeof(GateControl)) as GateControl[];
}
void Update()
{
foreach (GateControl ControlPanel in ControlsPanels)
{
if (ControlPanel.BroadcastChannel == BroadcastChannel && ControlPanel.SignalSent)
{
this.gameObject.transform.position += Movement;
if (MovementMethod == Method.MoveTowards)
{
this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, Movement, Time.deltaTime);
}
if (MovementMethod == Method.Lerp)
{
this.gameObject.transform.position = Vector3.Lerp(this.gameObject.transform.position, Movement, Time.deltaTime);
}
}
}
}
}
Not really answering your question but I’d strongly consider using something like LeanTwean (my favourite tweening engine) to do things like this - it creates vastly more simplified and readable code (and hence more maintainable).
Rather than check the channel in the update loop are you able to invert the control so that the messages are pushed to the receivers instead? So that when the event occurs you just notify all channel listeners and they act on that rather than polling the channel state every frame.
Every invocation of Update() has a cost, it’s best to avoid doing things via Update() when practical.
Unsure about what your recommendation means?
I think you are saying that I should be using custom methods to run this but not sure.
Something I am trying to find at the moment is a way to fall out of the if statement once completing the line once but I don’t want to do anything like that, I usually consider things like that to be last resorts.
UPDATE: I have tried setting ControlPanel.SignalSent to false after the line but that causes the object to instantly jump to the location, instead of MoveTowards or Lerp towards it.