Search Unity

How do i spawn a new GameObject(Player) while deleting the old one to maintain on one clonene at a

Discussion in 'Scripting' started by CF_Sixdotsstudios, Nov 3, 2020.

  1. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_12-59-46.png

    here's my code so far how can i better this for the above stated goal? i need the old or existing clone destroyed while the new one stays so there's only one GameObject of player at a time in the scene
     
  2. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    thankyou. let me try this. much appreciated...
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Make the if statement lowercase. My phone capitalized it and I didn’t see.
     
  4. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_15-7-17.png dioa
    upload_2020-11-3_15-7-17.png

    hi dios, thanks for helping me out. This is what to achieve, i need the spawn player to spawn at the playerPos(player position) which are objects i've placed in the scene for which i want the spawn to get the position and rotation from and match those so i control where the player spawns and what direction or rotation the have. when i press the button the player prefab should spawn at the respective playerPos and any given time there should only be the player spawn that is at the position that the button press represents.

    tried to modify the script as follows below but its not helping...
    upload_2020-11-3_15-12-55.png
     
  5. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    hi. i managed to correct that. picked upon it thanks.
     
  6. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Ah sorry I forgot a small yet important part. I edited the above code. Try that and let me know. I’m not at my computer to test but that should work.

    I think if you try destroying it before the edit it will throw an error about to prevent data loss etc.
     
    Last edited: Nov 3, 2020
  7. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    thanks let me try it
     
  8. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_15-51-22.png
    above is the way the button is set up...
    upload_2020-11-3_15-51-58.png
    Above is the object to which i've attached the script and how i've set it up...
    upload_2020-11-3_15-53-41.png
    above shows what happens everytime i click on a button to spawn and the respective player position. might be messing it up somewhere i dont know. newbie coder here...let me know what changes i should make. appreciate the help
     
  9. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay, It’s hard for me to read and look over everything clearly right now but we should get this working. Let’s start simple..

    If you click the spawn button, is it deleting the current GameObject that’s instantiated for the button or is it spawning multiple without deleting the current spawned? (Clone).

    I don’t see anything in your scene view that represents the spawn point and where the game object is being spawned at. Are they not spawning in the same location in the scene view?
     
  10. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    in answer to your first question yes its spawning a clone every time i click the button as shown in the image below. upload_2020-11-3_16-35-16.png

    in answer to the second question here below are the spawn positions. when i start the scene the player spawns at the position of the player position attached to the script. every button press keeps it spawning at the same place like in the image provided.
    upload_2020-11-3_16-38-19.png
    below is the player spawning at the same location every buttomnpress
    upload_2020-11-3_16-43-34.png

    below this is how i have set up the button
    upload_2020-11-3_16-44-47.png

    below is how i have set up the gameobject with the script attached
    upload_2020-11-3_16-45-33.png
     
  11. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    So if you click button (1) three times. Does only one clone appear or 3?

    the reason they are spawning in the same location is because you only have one spawn location setup in your script. Is the clone spawning at the spawn pos 1 location? Use a cylinder as player model to give you a visual instead of an empty transform
     
  12. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Also I edited the above code. So now it will not spawn one at the start. Use buttonpress as your buttons function instead of the spawnplayer
     
  13. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    View attachment 729000
    in answer to the first question they're appearing as many times as i click, so 3 click is 3 clones.
    in answer to the 2nd question they're spawning at position one cause that's the only position attached to the script. let me try the cylinder suggestion so we have a visual que to help
     
  14. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    thanks et me have a look...
     
  15. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay they should only have one clone appear per button click. Meaning if you click the button one it should spawn one, then if you click again it should delete the current and spawn a new.. so only one instance should exist per button. So I will look back over the code again.
     
  16. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    how do i make a provision for multiple spawn positions with the code we have at the moment? it only has room for one spawn position check image below. as an update now its only spawning once per button click. progress at last
    upload_2020-11-3_17-20-21.png
     
  17. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay so changing the button function fixed it? I don’t usually use built in I hard Code mine so I was hoping changing the button function would work.. so for the spawn points. You’ll want to do the following..

    For each of your spawn points, make them a child of your spawn manager. Make sure they are the only child objects. I’ll work up the code while you do that and post it. Give me a few minutes as it’ll take me a little bit to do right now on my phone lol
     
  18. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    now its doing that. with your last edit. there's only ever one clone in the scene at a time. we just need to work on making it spawn to the different points.

    as an update when i start the scene and click on a button it always goes to the spawn point attached at the time...i duplicated the script in the scene 4 time and attach each to a respective button n on scene start it moves to whatever button i press first n then there's not more movement or cloning done
     
  19. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    yes, changing the button fixed things. cool. no problem will wait on u. thanks again...
     
  20. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    done making the spawn point the children of the spawn manager.
    upload_2020-11-3_17-34-3.png
     
  21. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay I edited the above code it should work. Place all your spawn buttons into an empty game object and drag that empty game object Into “Spawners”. You’ll want to indent everything correctly though. I couldn’t get it to do properly from my phone. It may need some tweaks to function right but try that as of now and get back to me with what happens and then I’ll be able to set it right. Like I said I don’t have a computer to test it for functionality
     
  22. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    that's fine. let me try what u've suggested thus far. will back in bit
     
  23. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    these are the errors so far...
    upload_2020-11-3_18-26-1.png
     
  24. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Lol yeah doing this on phone driving isn’t easy... Let me go fix real quick
     
  25. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay should be fixed now. Copy and paste the entire thing and then fix the indenting.

    when you pasted a few lines you forgot a bracket
     
  26. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    Lol... Cool will wait on u. I understand. Serious newbie here. I get the logic still learning the syntax... Architectural background.
     
  27. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Woops forgot to correct the spawn info function. Just did that so that should be good now. Sorry for the mistakes man I’m driving so I’m not really reading over everything or double checking
     
  28. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    No worries man. Ur helping a tone. Will be back in a bit
     
  29. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    one last set of errors...
    upload_2020-11-3_19-0-13.png

    managed the others except the above
     
  30. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I just made the change above. I was thinking of list not arrays. Look above at edit and try that
     
  31. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    col
    cool. will do
     
  32. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    the good news is there's no errors and we only have one cylinder at any given time. plus immediately after running the scene the cylinder spawns to the relevant cylinder position. though pressing the subsequent buttons does nothing. below is the setup for the spawn manager and the buttons just incase am doing something wrong

    spawn manager
    upload_2020-11-3_19-25-13.png

    buttons
    upload_2020-11-3_19-25-53.png

    we almost there...
     
  33. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_19-27-2.png
    the reds are the spawnpoints
     
  34. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Debug.log(I);


    place that right beloW
    Code (csharp):
    1. if(RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition, Camera.main)
    see if it prints anything in console. It should be a different number for each button
     
  35. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool. let me try that
     
  36. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    that's the problem area. its showing the same number every time i press a button
    upload_2020-11-3_19-42-40.png
     
  37. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Aha I figured... let me get out of traffic and I’ll send an update
     
  38. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Just to be sure do
    Code (csharp):
    1.  
    2. Debug.log(Spawner[I]);
    3.  
    4.  
     
  39. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool...thanks...
     
  40. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    let me try that
     
  41. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_19-51-40.png
    this is what am getting
     
  42. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Same for every button?
     
  43. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Post the entire code here using tags so I can edit my original post. It’ll make it easier for me to look at
     
  44. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    yes pressed everyone and that's the only log am getting...
     
  45. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool...

    "
    public Transform[] SpawnPoint;

    public Button[] Spawner;

    public GameObject Spawners;

    public GameObject player;

    void SetSpawnInfo()
    {
    SpawnPoint = this.GetComponentsInChildren<Transform>();
    Spawner = Spawners.GetComponentsInChildren<Button>();
    }

    void ButtonPress()
    {
    SpawnPlayer(player);
    }

    public void SpawnPlayer(GameObject newPlayer)
    {
    for (int I = 0; I < Spawner.Length; I++)
    {
    if (RectTransformUtility.RectangleContainsScreenPoint(Spawner.transform as RectTransform, Input.mousePosition,
    Camera.main))
    {
    if (newPlayer != null)
    {
    Destroy(newPlayer);
    }

    newPlayer = player;

    if (newPlayer != null)
    {
    newPlayer = Instantiate(newPlayer, SpawnPoint.position, SpawnPoint.rotation);
    Debug.Log(I);
    Debug.Log(Spawner);
    }
    }


    }
    }
    "
     
  46. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Place it inside code tags so it’ll format
     
  47. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    Code (CSharp):
    1. public Transform[] SpawnPoint;
    2.  
    3.     public Button[] Spawner;
    4.  
    5.     public GameObject Spawners;
    6.  
    7.     public GameObject player;
    8.  
    9.     void SetSpawnInfo()
    10.     {
    11.         SpawnPoint = this.GetComponentsInChildren<Transform>();
    12.         Spawner = Spawners.GetComponentsInChildren<Button>();
    13.     }
    14.  
    15.     void ButtonPress()
    16.     {
    17.         SpawnPlayer(player);
    18.     }
    19.  
    20.     public void SpawnPlayer(GameObject newPlayer)
    21.     {
    22.         for (int I = 0; I < Spawner.Length; I++)
    23.         {
    24.             if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    25.                 Camera.main))
    26.             {
    27.                 if (newPlayer != null)
    28.                 {
    29.                     Destroy(newPlayer);
    30.                 }
    31.  
    32.                 newPlayer = player;
    33.  
    34.                 if (newPlayer != null)
    35.                 {
    36.                     newPlayer = Instantiate(newPlayer, SpawnPoint[I].position, SpawnPoint[I].rotation);
    37.                     Debug.Log(I);
    38.                     Debug.Log(Spawner);
    39.                 }
    40.             }
    41.  
    42.  
    43.         }
    44.     }
     
  48. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Wait... I think I just seen the issue.

    change this
    Code (csharp):
    1. (RectTransformUtility.RectangleContainsScreenPoint(Spawner.transform as RectTransform, Input.mousePosition,
    2. Camera.main))[
    To this
    Code (csharp):
    1.  
    2. (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    3. Camera.main))
    4.  
    5.  
     
  49. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool...
     
  50. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    We have the following
    upload_2020-11-3_20-9-25.png