Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by exrescueswimmer · Dec 11, 2015 at 01:54 PM · gameobjectscript.prefabslists

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

First, thanks for any help here - I'm reluctant to ask because it's probably something obvious I'm missing but i've been looking at it too long...

The scenario is I have a holder object with a pathfinding script (working fine) and an event-based assignment script.

The objects being assigned are a group of instantiated prefab characters with a script added that navigates the path calculated from the pathfinding script when assigned from the assignment script.
If they all follow the same exact path, it's working great.

What I am trying to do is append a unique "final location" for each prefab. Think of a bridge crew running on to the bridge then breaking up to battle stations.
Here is the simplified assignment script:

public List<Vector3> RunPath; //populated by the pathfinding script public bool ExecutePath;//set by the pathfinding script once the entire path is calculated public GameObject[] CharacterArray; void Start () { %|-147361493_1|% %|-950286578_2|% } void Update () { %|1226425402_3|% %|737211570_4|% %|-481316925_5|% %|1764865785_6|% %|177424711_7|% %|882064304_8|% %|306964567_9|% %|-1581774797_10|% %|-958725657_11|% %|-1565622815_12|% %|-1720740372_13|% %|130943564_14|% %|-98425953_15|% } The issue is every prefab has a public List Path; variable and every prefab is getting every vector3 added to Path from CharacterFinalPositions[intClassCount]) which is the List of unique final positions. In other words, each prefab should get just one but instead, each prefab is getting them all.

Thanks again for any advice.

Comment
Add comment · Show 3
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 exrescueswimmer · Dec 11, 2015 at 01:01 AM 0
Share
 public List<Vector3> ClassPath;//populated by the pathfinding script
 public bool ExecutePath;//set by the pathfinding script once the entire path is calculated
 public GameObject[] CharacterArray;
 
 void Start () 
 {
     CharacterArray = GameObject.FindGameObjectsWithTag("Student");
     ExecutePath = false;
 }
 
 void Update () 
 {
     if (ExecutePath == true)
     {
         int intClassCount = 0;
         foreach (GameObject nextCharacter in CharacterArray)
         {
             nextCharacter.GetComponent<FollowPath>().Path = ClassPath;//list of vector3s
             nextCharacter.GetComponent<FollowPath>().Path.Add(CharacterFinalPositions[intClassCount]);
             //CharacterFinalPositions is a list of unique vector 3s
             
             nextCharacter.GetComponent<FollowPath>().AtTarget = false;
             
             intClassCount++;
         }
 
 
     }
     
     ExecutePath = false;
 }
avatar image exrescueswimmer · Dec 11, 2015 at 01:02 AM 1
Share

code formatted weird...

avatar image exrescueswimmer · Dec 13, 2015 at 06:46 PM 0
Share

no one has any thoughts?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LazyElephant · Dec 13, 2015 at 09:28 PM

 nextCharacter.GetComponent<FollowPath>().Path = ClassPath;

This line doesn't create a unique copy of the list for each object, it just assigns a reference to the list you already have. Since all the objects are referencing the same list, all the endpoints get added to that single list and the objects all follow the same path.

You will have to make sure you are creating a new list for each object before you add the unique end point to it.

This will, of course, create a lot of lists with mostly the same elements and waste memory. As an alternative, you could give all your objects a reference to the base ClassPath list, but store the endpoints in a separate Vector3 variable for each object.

Comment
Add comment · Show 1 · 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
avatar image exrescueswimmer · Dec 14, 2015 at 10:45 PM 0
Share

makes perfect sense.

thanks again!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

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

How to not have a function affect a prefab? 2 Answers

Create with code 2.2 pizza not shooting 1 Answer

Prefab doesn't keep it's public variable when Instantiated? 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