Search Unity

Make a List and choose a variable

Discussion in 'Scripting' started by Elverion, May 2, 2016.

  1. Elverion

    Elverion

    Joined:
    Feb 1, 2016
    Posts:
    5
    Hi Everyone !

    First, sorry for my bad english, i'm a french guy (tour Eiffel et baguette !)

    So, for a personnal project i make a game where you control a carnivor plant by throwing his head in the level for eat some guy !
    You can only throw him and when your plant velocity is over, a new plant grow at this point and you can throw him again !

    Currently i program the plant system, i instantiate a checkpoint each time she don't move and when plant dead by guy or void she teleport at last checkpoint, i instantiate a new plant each time i throw it, ennemy, point etc is already program BUT ! I want a system where you can choose between ALL the plant you create !
    If you throw your plant 4 times, i want player can throw him again with the second or third plant he create (in fact create a list and player choose between all plant in the list for his starting point)

    I'm a begginer and i dont understand how the List System work in c#... i saw the tutorial of Unity Dev but i don't understand all the tuto =p

    If someone can help me it would be so great =D

    I can post my scripts but there are 8-10 scripts xD

    Thank in advance
     
  2. Kazen

    Kazen

    Joined:
    Feb 17, 2015
    Posts:
    68
    What don't you get about lists? a list contains your variables in a specific order. Here's a simple sample of how to use lists:
    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. List<GameObject> listOfGameObjects = new List<GameObject>();
    4.  
    5. public void AddObjectToList(GameObject objectToAdd)
    6. {
    7. // this adds the object at the end of the list
    8. listOfGameObjects.Add(objectToAdd);
    9. }
    10.  
    11. public void RemoveGameObject(GameObject objectToRemove)
    12. {
    13. // this assumes that the object is actually inside the list, otherwise, nothing happens (or maybe it throws an error, don't remember)
    14. listOfGameObjects.Remove(objectToRemove);
    15. }
    16.  
    17. public GameObject GetGameObject(int index)
    18. {
    19. // this returns the gameobject that exists at "index" position.
    20. // This means, if index is 0, the first object added will be returned, 1 means the one after that and so on.
    21. return listOfGameObjects[index];
    22. }
     
    Kiwasi likes this.
  3. Elverion

    Elverion

    Joined:
    Feb 1, 2016
    Posts:
    5
    Well, thank you i will try again with your method ^^
     
  4. Elverion

    Elverion

    Joined:
    Feb 1, 2016
    Posts:
    5
    Well, i try, it's better, but not easy xD

    How i can add to my list an instantiate plant and choose one of this plant with arrow for exemple (left previous plant right next plant) for throw my plant again at the same position ?

    not sure if it's clear but i want all my plant like a checkpoint and player choose the checkpoint for throw is plant again =)
     
  5. Kazen

    Kazen

    Joined:
    Feb 17, 2015
    Posts:
    68
    When you instantiated your plant, send it to your list with the add method
    GameObject plant = GameObject.instantiate(...)
    AddObjectToList(plant);

    store a variable with the index of the current plant you have selected and and you can do whatever you want with it.