- Home /
List not working when multiple transforms inserted
I'm trying to create a script that contains an ArrayList of transforms. I have an object that follows my player and collects cans when it gets to a certain distance from them. I can get the object to collect 1 single can, but when I add another transform to my array list, the object that is supposed to collect the cans won't detect the cans, and goes in a different direction. Any ideas why this is happening?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BatChaser : MonoBehaviour {
public Transform trash;
public List<Transform> trash1 = new List<Transform>();
//script disabler lag behind
public LagBehind lagBehind;
public float trashDistance;
public float rotationDampening;
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
//lag behind disable
lagBehind = GetComponent<LagBehind>();
}
// Update is called once per frame
void FixedUpdate()
{
{
}
for (int i = 0; i < trash1.Count; i++)
{
//this line below fixes the deleted object reference
if (trash1[i] != null)
{
trashDistance = Vector3.Distance(trash1[i].position, transform.position);
if (trashDistance < 20f)
{
lookAtTrash();
}
if (trashDistance < 15f)
{
lagBehind.enabled = false;
chase();
}
}
lagBehind.enabled = true;
}
}
void lookAtTrash()
{
for (int i = 0; i < trash1.Count; i++)
{
Quaternion rotation = Quaternion.LookRotation(trash1[i].position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDampening);
}
}
void chase()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
Answer by KaspianR · Dec 06, 2019 at 08:14 AM
You would probably want to only make your object only look at the trash within it's range using something like this:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BatChaser : MonoBehaviour {
public Transform trash;
public List<Transform> trash1 = new List<Transform>();
//script disabler lag behind
public LagBehind lagBehind;
public float trashDistance;
public float rotationDampening;
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
//lag behind disable
lagBehind = GetComponent<LagBehind>();
}
// Update is called once per frame
void FixedUpdate()
{
for (int i = 0; i < trash1.Count; i++)
{
//this line below fixes the deleted object reference
if (trash1[i] != null)
{
trashDistance = Vector3.Distance(trash1[i].position, transform.position);
if (trashDistance < 20f)
{
lookAtTrash(i);
}
if (trashDistance < 15f)
{
lagBehind.enabled = false;
chase();
}
}
lagBehind.enabled = true;
}
}
void lookAtTrash(int i)
{
Quaternion rotation = Quaternion.LookRotation(trash1[i].position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDampening);
}
void chase()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
But note that it still will only chase the first object in the list that's within it's range and not necessarily the closest one.
Also, I just have to point out that you should either run everything in Update()
or use Time.fixedDeltaTime
. Otherwise people with slower machines will have a much faster object compared to everyone else since Time.deltaTime
is the time between Update()
's. Hope this solves your problem, good luck!
Your answer
Follow this Question
Related Questions
C#, code stops loop prematurely 1 Answer
Get all GameObjects by variable value 2 Answers
Play Animation Elements List 0 Answers
List or Array? 2 Answers