• 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.

Show more comments

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our 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

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

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

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

how can i access to the scripts which have same name and were attached to the same gameobject. 2 Answers


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