Search Unity

Pick up objects with random

Discussion in 'Scripting' started by Duttget233, Dec 2, 2016.

  1. Duttget233

    Duttget233

    Joined:
    May 10, 2016
    Posts:
    6
    Hi guys i need help with this:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. publicclassRandomme:MonoBehaviour{
    6.  
    7. publicfloatminTime=1f;
    8. publicfloatmaxTime=2.0f;
    9. publicGameObject[]Item;
    10. publicTransform[]Spawn=newTransform[3];
    11. publicboolRandomX=false;
    12. publicfloatminX=-2f,maxX=2f;
    13.  
    14. voidCreateObject(Vector3position,GameObjectprefab)
    15. {
    16. if(RandomX)
    17. position+=newVector3(Random.Range(minX,maxX),0,0);
    18. Instantiate(prefab,position,Quaternion.identity);
    19. }
    20. voidStart(){
    21. Negro();
    22. boolplaceObstacle=Random.Range(0,2)==2;
    23. intobstacleIndex=4;
    24. if(placeObstacle)
    25. for(inti=0;i<Spawn.Length;i++){
    26. if(i==obstacleIndex)
    27. continue;
    28. if(Random.Range(2,2)==2){
    29. CreateObject(Spawn[i].position,Item[Random.Range(0,Item.Length)]);
    30. }
    31. }
    32. }
    33. voidUpdate(){
    34.  
    35.  
    36. }
    37. voidNegro(){
    38. intIndiceObjetos=Random.Range(0,5);
    39. intIndicepawnbs=Random.Range(0,3);
    40.  
    41. intsecondRandomObject=Random.Range(0,5);
    42. while(secondRandomObject==IndiceObjetos)
    43. {
    44. secondRandomObject=Random.Range(0,5);
    45. }
    46. intsecondRandomPawn=Random.Range(0,3);
    47. while(secondRandomPawn==Indicepawnbs)
    48. {
    49. secondRandomPawn=Random.Range(0,3);
    50. }
    51.  
    52.  
    53.  
    54. Instantiate(Item[IndiceObjetos],Spawn[Indicepawnbs].position,Spawn[Indicepawnbs].rotation);
    55. Instantiate(Item[secondRandomObject],Spawn[secondRandomPawn].position,Spawn[secondRandomPawn].rotation);
    56. }
    57.  
    58. }
    59.  
    60.  
    Now I want to pick up those 2 obj that random give in a specific order. If u take it in specific order u win but if u don't take it in the specific order u lose. How i do that?
     
  2. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    my friend, could you please format a bit your code.. to check better where is the problem.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    One quick note. Random.Range when working with integers, the second argument is *exclusive* meaning it will pick a number lower than the 2nd argument. So this line:
    Code (CSharp):
    1. bool placeObstacle=Random.Range(0,2)==2;
    \
    will always be false, since random.Range will only return 0, or 1.

    Now for your code to check if they pick up the objects in the right order you need to save a reference to the objects you make:

    Code (CSharp):
    1. private GameObject firstChoice;
    2. private GameObject secondChoice;
    3.  
    4. void Negro()
    5. {
    6.        // other code
    7.         firstChoice = Instantiate(Item[IndiceObjetos],Spawn[Indicepawnbs].position,Spawn[Indicepawnbs].rotation);
    8.     second Choice = Instantiate(Item[secondRandomObject],Spawn[secondRandomPawn].position,Spawn[secondRandomPawn].rotation);
    9. }
    Then you can check if the objects your player chooses is equal to first or second choice