Search Unity

Change Amount value of objects in List.

Discussion in 'Scripting' started by rogueghost, Nov 4, 2018.

  1. rogueghost

    rogueghost

    Joined:
    Jun 2, 2018
    Posts:
    76
    Hello.

    I'm trying to instantiate a List of GameObjects that have a equal script attached to them, add the objects to another List, compare if the new List have equal objects by its Name string and if true increase a int value named Amount of the equal objects, but i can't figure out a way to do this.
    Here is what i've done so far:

    Code (CSharp):
    1.  
    2. public class ScriptOne : Monobehaviour {
    3. public void InstantiateList (List <GameObject> objsList, int objAmount)
    4. {
    5. //This list contains various objets that have the same script attached to them but with
    6. //different values.
    7. //I want to increase the Amount value only for the equal named objects.
    8. for (int i = 0; i < objsList.Count; i++) {
    9. if (objsList[i].gameObject.name == nameToLookFor) {
    10. //The code below sets the Amount in the SomeScript attached to the objsList to objAmount.
    11. //But it's the same value for each of them...
    12. //What i want to do is "stack" the Amount value for each equal object found.
    13. objsList[i].gameObject.GetComponent <SomeScript> ().Amount = objAmount;
    14. newObjectsList.Add (objsList[i]);
    15. }
    16. }
    17. }
    18.  
    19. public class ScriptTwo : MonoBehaviour {
    20. //Here is the function in the other script that calls InstantiateList:
    21. public void AddToInstantiateList ()
    22. {
    23. for (int i = 0, i < cubesList.Count; i++){
    24. if (cubesList[i].GetComponent <CubeBehaviour> ().isSelected == true{
    25. List <GameObject> firstList = new List <GameObject>;
    26. //Is there a way to store the "amount" value of each object in "cubesList"?
    27. int amount = cubesList[i].GetComponent <CubeBehaviour> ().theAmount;
    28. firstList.Add (cubesList[i]);
    29. //Then get the values at InstantiateList() function at ScriptOne?
    30. //And assign these values to each SomeScript component attached to the objsList //GameObjects at ScriptOne if their name is equal?
    31. scriptOne.InstantiateList (firstList[i], amount) //maybe turn amount in another list?
    32. }
    33. }
    34. }
    35. }
    36. }
    Maybe i can create a List of integers in
    ScriptTwo
    AddToInstantiateList ()
    to store the
    Amount
    values of each object in
    cubesList
    and pass it as argument for
    InstantiateList
    in
    ScriptOne
    ? Then how i would get and set the Amount int
    InstantiateList
    for each object of the same Name?
     
    Last edited: Nov 4, 2018
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I don't really understand what you're asking here. Are you wanting the each object's
    Amount
    value to increase by one when added to
    newObjectsList
    ?

    Like this?

    newObjectsList[0].Amount = 0

    newObjectsList[1].Amount = 1

    newObjectsList[2].Amount = 2

    newObjectsList[3].Amount = 3

    newObjectsList[4].Amount = 4

    Etc...
     
  3. rogueghost

    rogueghost

    Joined:
    Jun 2, 2018
    Posts:
    76
    I'm calling the InstantiateList (List <GameObject> objsList, int objAmount) from another script and i'm trying to figure out a way to increase the variable Amount from <SomeScript> attached to all objects in the objsList that has the same Name with a loop, and if the names match i don't instantiate a new object, i just add up to its amount.
    In the other script that i'm calling this function i'm looping in a List of selected objects and passing the List as argument. I think the argument objAmount is too limited for what i'm trying to achieve...
     
    Last edited: Nov 4, 2018
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I don't see anything wrong with your code then.
    Does
    newObjectsList
    have any objects after the method completes? If not, then perhaps none of the object's names from the original list are matching with
    nameToLookFor
    .
     
    rogueghost likes this.
  5. rogueghost

    rogueghost

    Joined:
    Jun 2, 2018
    Posts:
    76
    I edited my last post, i think it's a bit more clearer.
     
  6. rogueghost

    rogueghost

    Joined:
    Jun 2, 2018
    Posts:
    76
    Does anyone have a solution to this? I just need to add the correct
    Amount
    value to each objects in the
    objsList[i]
    after checking if their name is equal to
    nameToLookFor
    .
    How do i store these
    Amount
    values in
    ScriptTwo
    AddToInstantiateList
    and pass it to
    ScriptOne InstantiateList()
    and assign them correctly to the
    objsList[i]
    ?
     
  7. rogueghost

    rogueghost

    Joined:
    Jun 2, 2018
    Posts:
    76
    That's almost it! I was thinking of using a new
    List <int> objsAmount

    to store the
    Amount
    value of each object that has the
    CubeBehaviour
    and use it as argument for the
    InstantiateList()
    function in
    ScriptOne
    . But i don't know how to use this new List to assign the
    Amount
    value to each
    objsList[i] List
    that matches with
    NameToLookFor
    automatically.
     
    Last edited: Nov 4, 2018