Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Apr 29, 2020 at 02:21 PM by kianniek for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kianniek · Apr 29, 2020 at 11:17 AM · gameobjectscript.getcomponentlists

problem with a list that has to store different things for multiple gameobjects

i have two gameobject with both a task_list that they can follow but when i add a task to one of the gameobjects it gets also added to the other one how could i prevent that (they both use the same list but i want them to store different tasks into them)

     public class PriorityList : MonoBehaviour
     {
     
         public string CurrentTask = "";
         public Vector3 CurrentTaskLocation;
         public List<string> TaskList;
         public List<Vector3> TaskLocation;
         public float timeLeft = 15.0f;
         private TaskObjects taskObjects;
         private NavMeshAG navMeshAG;
     
         private void Start()
         {
     
             navMeshAG = gameObject.GetComponent<NavMeshAG>();
         }
         void Update()
         {
             if (timeLeft <= 1 && TaskList.Count == 0 && TaskLocation.Count == 0)
             {
                 CurrentTask = "";
                 timeLeft = 1f;
             }
             if (CurrentTask == "Vullen")
             {
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 3f;
                 }
             }
             if (CurrentTask == "klanthelpen")
             {
                 //times will differ from costemer to custemer
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 0.5f;
                 }
             }
             if (CurrentTask == "splitsen")
             {
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 1.5f;
                 }
             }
             if (CurrentTask == "Pauze")
             {
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 3.5f;
                 }
             }
             if (CurrentTask == "kassa")
             {
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 3f;
                 }
             }
             if (CurrentTask == "spiegelen")
             {
                 
                 timeLeft -= Time.deltaTime;
                 if (timeLeft <= 0)
                 {
                     CurrentTask = TaskList[0];
                     TaskList.RemoveAt(0);
                     CurrentTaskLocation = TaskLocation[0];
                     TaskLocation.RemoveAt(0);
                     timeLeft = 60f * 2f;
                 }
             }
         }
     
     }

Comment
Add comment · Show 2
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
avatar image zohaibzaidi · Apr 28, 2020 at 09:02 AM 0
Share

If each of your gameobject has a script on it which has a task_list then this should not be the case. Can you show some code, that'll make it easier to see what is going wrong.

avatar image kianniek zohaibzaidi · Apr 28, 2020 at 09:11 AM 0
Share

this is the navigation script: public class Nav$$anonymous$$eshAG : $$anonymous$$onoBehaviour { public Nav$$anonymous$$eshAgent agent; public Vector3 startPossision; public Vector3 TargetPosition; public bool alreadyInlist = false; public Nav$$anonymous$$eshAG nav$$anonymous$$esh; public PriorityList priorityList;

     int layer_mask;
     // Start is called before the first frame update
     void Start()
     {
         layer_mask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Vloer");
         nav$$anonymous$$esh = gameObject.GetComponent<Nav$$anonymous$$eshAG>();
         priorityList = gameObject.GetComponent<PriorityList>();
         agent = gameObject.GetComponent<Nav$$anonymous$$eshAgent>();
         nav$$anonymous$$esh.enabled = true;
     }
     // Update is called once per frame
     public void Update()
     {
 
         
         if (Input.Get$$anonymous$$ouseButtonDown(2))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit/*, layer_mask*/))
             {
                 TargetPosition = hit.point;
                 Debug.Log(hit.point);
                 
                 if (agent.remainingDistance <= 1)
                 {
                     nav$$anonymous$$esh.enabled = true;
                 }
 
                 if(hit.collider.gameObject.TryGetComponent(typeof(TaskObjects), out Component component))
                 {
                     Debug.Log("found it in" + hit.collider.name);
                     if(priorityList.CurrentTask == null || priorityList.CurrentTask == "")
                     {
                         
                         priorityList.CurrentTask = hit.collider.gameObject.GetComponent<TaskObjects>().What_Am_I_Used_For;
                     }
                     else
                     {
                         //contain the task and the location of the task in Script(priorityList)
                         priorityList.TaskList.Add(hit.collider.gameObject.GetComponent<TaskObjects>().What_Am_I_Used_For);
                     }
                     
                     if (priorityList.CurrentTaskLocation == null || priorityList.CurrentTaskLocation == Vector3.zero)
                     {
                         priorityList.CurrentTaskLocation = hit.collider.gameObject.GetComponent<TaskObjects>().gameObject.transform.position;
                     }
                     else
                     {
                         //contain the task and the location of the task in Script(priorityList)
                         priorityList.TaskLocation.Add(hit.collider.gameObject.GetComponent<TaskObjects>().gameObject.transform.position);
                     }
                 }
             }
             //agent.SetDestination(startPossision + new Vector3(1, 0, 0));
 
             agent.SetDestination(priorityList.CurrentTaskLocation);
         }
     }
 }


1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by highpockets · Apr 28, 2020 at 09:02 PM

I think what you are doing is putting the list on a game object in the hierarchy and then adding it to the public variables in the inspector of the 2 objects that require the list. If this is correct. You are using the same instance of the list for both game objects that are referencing it.


To avoid this behaviour, you need to have a separate instance of the list for each object. Just add the list as a script/component on your object in the inspector.


If you for some reason need to ensure that the list only has one instance, you would have to make custom logic within the list to ensure the correct game objects have access to the correct items, but this seems a bit silly.

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

Follow this Question

Answers Answers and Comments

218 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 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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Get specific component on gameobject after order in inspector 1 Answer

Prefab Script List not being assigned as unique - can you see why? 1 Answer

Accessing Variables from another object Script 1 Answer

How to detect if two objects become a square ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges