Search Unity

Random without objects fall to the same places #HELP

Discussion in 'Scripting' started by aurefelin, Jun 25, 2017.

  1. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    Hi, I shall need help for a script that I find no help(assistant) somewhere else:
    make a system of random without objects fall to the same places.

    But nobody few to help me :'( I really need help.
    Thank you for your answer.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You didn't post anything, so how can we help you? What have you tried already? Show us some code and we can help you along if it's not working the way you want. Most people aren't just going to just write a script for you. (many reasons for this)
     
    cstooch likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Indeed.. One small tidbit of help might be, once you try a script and you don't want something to be at the same location, you can execute a small sphere/cube cast or something similar to ensure that no other object is present.
    In case you have like 20 different "spots", for example, but only 10 objects you place randomly, you could simply remove each "spot" from the list, and take a new random one, thus ensuring that any previously chosen spot is not re-used.
    :)
     
  4. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    Ho, sorry. Then my project is when I press a button, GameObjects puts itself on the points of appearances without they are on the same place (For example: the distribution of cards).

    I want that my GameObject mixes with the other GameObjects but without they are on the same place. For me it is impossible to make a script like that, but I really need it. Thank you if you find solution to my problem.

    Here is I tried that but that does not work, my GameObject do not want to move. :'(

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Randomcard : MonoBehaviour {
    7.  
    8.         public Transform[] spawns;
    9.         public GameObject Bone;
    10.         public int amountThings = 2;
    11.         public int i = 0;
    12.  
    13.  
    14.         void Start()
    15.         {
    16.             Spawn();
    17.         }
    18.  
    19.  
    20.         public void Spawn()
    21.         {
    22.             List<Transform> freeSpawnPoints = new List<Transform>(spawns);
    23.             for (i = 0; i < amountThings; i++)
    24.             {
    25.                 if (freeSpawnPoints.Count <= 0)
    26.                     return; // Not enough spawn points
    27.                 int index = Random.Range(0, freeSpawnPoints.Count);
    28.                 Transform pos = freeSpawnPoints[index];
    29.                 freeSpawnPoints.RemoveAt(index);
    30.                 Instantiate(Bone, pos.position, pos.rotation);
    31.             }
    32.         }
    33.  
    34. }
    I am sorry for my bad english :)
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That looks more or less right to me.
    So, what happens with this code? Does it instantiate a new object, but the position it goes to isn't correct?
     
  6. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    So, as soon as I press the button to execute the script, it GameObject do not want to move on points planned.
     
  7. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    I want a script which allows GameObjects to move on 4 points, everything in random and without they are in the same places.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not sure if you answered my question or not, sorry.
     
  9. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    I answered your question. The problem it is that texts "Bonjour1", "Bonjour2", "Bonjour3" and "Bonjour4 "do not want to mix and to move on points "sp "," sp (1) "," sp (2) " and " sp (3)", without texts are all in the same places.

     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, okay.. You have 1 object you're instantiating in the code.
    The loop runs twice, again in your code, unless the value of '2' was changed to 4 in the inspector.

    But other than that, assuming that your positions are correctly set, your code looks good.
    So, I was trying to talk through about where the issue is. :)
     
  11. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    I does not understand, you could make an example? Because I am French and I does not understand quite the themes in English, thanks to you :)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Here, try this:
    Code (csharp):
    1.  
    2. public void Spawn()
    3.         {
    4.             List<Transform> freeSpawnPoints = new List<Transform>(spawns);
    5.             for (i = 0; i < amountThings; i++)
    6.             {
    7.                 print("Loop #: " + i);
    8.                 if (freeSpawnPoints.Count <= 0)
    9.                     return; // Not enough spawn points
    10.                 int index = Random.Range(0, freeSpawnPoints.Count);
    11.                 Transform pos = freeSpawnPoints[index];
    12.                 freeSpawnPoints.RemoveAt(index);
    13.                 print("Random position selected: " + pos.position);
    14.                 Instantiate(Bone, pos.position, pos.rotation);
    15.             }
    16.         }
    17.  
    Look at the logs in the console.
     
  13. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did that help? I mean, is that where they went? those positions listed there?
     
  15. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    No they did not move :(
     
  16. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    I'm desperate :'(
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Alright, well if you can make a simple package with just this little bit we're talking about. You can post it here and I will download it and take a look at it and see what's wrong, if you'd like.
    You can post the unity package right on the forums.
    If that fails for any reason, you can zip it and upload it to dropbox or onedrive, and paste the link.
    I might see it tonight or tomorrow. Unless you get some help that fixes your issue, I'm not sure what else to say.
     
  18. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    It is good, I put the file as attachment :)
     

    Attached Files:

  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm checking it out now.
     
  20. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    I forgot to explain, but the script I do not know where to put him(it)
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, i saw the script was no where lol.
    I just put it on SpawnPoints (gameobject).
    Code (csharp):
    1.  
    2. public class RandomTxt : MonoBehaviour
    3. {
    4.     public List<Transform> spawns;
    5.     [SerializeField]
    6.     List<GameObject> TextObjs; // all text objects
    7.     public Transform canvasTransform; // canvas
    8.  
    9.     void Start()
    10.     {
    11.         if(spawns.Count < TextObjs.Count)
    12.         {
    13.             print("Error, not enough spawn points compared to text objects.");
    14.             return;
    15.         }
    16.         Spawn();
    17.     }
    18.  
    19.  
    20.     public void Spawn()
    21.     {
    22.        // one of the changes I made was to remove the separate array and list for the spawn points.
    23.        // this makes no difference in the code that I'm aware of. If you need to call 'Spawn()' again,
    24.        // you may want to re-add the array (or have a second list) -- so you can re-use it.
    25.         for (int i = 0; i < TextObjs.Count; i++)
    26.         {
    27.             int index = Random.Range(0, spawns.Count);
    28.             Transform pos = spawns[index];
    29.             spawns.RemoveAt(index);
    30.             GameObject nextObj = Instantiate(TextObjs[i], pos.position, pos.rotation);
    31.             nextObj.transform.SetParent(canvasTransform);
    32.         }
    33.     }
    34. }
    35.  
    You can read my comments. You have to make sure you fill the parts in the inspector properly.
    This code is working for me.
     
    cstooch likes this.
  22. aurefelin

    aurefelin

    Joined:
    May 29, 2017
    Posts:
    17
    Wow, I do not know how to thank you, an enormous thank you Methos5k.

    Merci, merci, tu es mon sauveur, je ne savais plus quoi faire, et tu m'as sauvé. :);)

    After this beautiful sentence in French, I have to let you save from other young miserable as me :p

    Good luck and good luck for the continuation, goodbye my friend :rolleyes:
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome. Take care :)