• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by adamanderson16 · Dec 06, 2019 at 05:24 AM · array list

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Crate an array associative from a php return array 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges