Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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
    Code (CSharp):
    1. public int S;
    2.  
    3.     public Transform[] SpawnPoint;
    4.  
    5.     public Button[] Spawner;
    6.  
    7.     public GameObject Spawners;
    8.  
    9.     public GameObject player;
    10.  
    11.     void Awake()
    12.     {
    13.         SetSpawnInfo();
    14.     }
    15.    
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonUp(0))
    19.             {
    20.             //place here
    21.                 for (int I = 0; I < Spawner.Length; I++)
    22.                 {
    23.                 if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    24.                     Camera.main))
    25.                     {
    26.                         S = I;
    27.                         SpawnPlayer(player);
    28.                     }
    29.                 }
    30.             }
    31.     }
    32.  
    33.     void SetSpawnInfo()
    34.     {
    35.         SpawnPoint = this.GetComponentsInChildren<Transform>();
    36.         Spawner = Spawners.GetComponentsInChildren<Button>();
    37.     }
    38.  
    39.     public void SpawnPlayer(GameObject newPlayer)
    40.     {
    41.         if (newPlayer != null)
    42.         {
    43.             Destroy(newPlayer);
    44.         }
    45.  
    46.         newPlayer = player;
    47.  
    48.         if (newPlayer != null)
    49.         {
    50.             newPlayer = Instantiate(newPlayer,
    51.             SpawnPoint[S].position,
    52.             SpawnPoint[S].rotation);
    53.         }
    54.  
    55.        
    56.     }
    there you go
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I’m honestly stumped. I use that update method for a lot of my functions and have never had an issue. Especially anything being removed from the component... is your cylinder object made into a prefab? And then dropped into the script? I’m assuming yes but I am dumb founded right now
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Is there a space between SpawnPoint and on lines 51, and 52?

    I’m not sure if that would cause errors like that but if so remove the space and Try it
     
  4. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    oh my bad... the cylinder isnt a prefab...let me make one..be right back
     
  5. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    upload_2020-11-3_23-19-1.png
    its now spawning but making multiple copies instead of maintaining one clone
    below is the log
    upload_2020-11-3_23-20-10.png
     
  6. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay that error log means is trying To delete the prefab now. So make sure there is no instance of the prefab in the scene and that the prefab is set into the player input on the script
     
  7. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    oh okay. i did that before those logs as well,what might i be missing?
     
  8. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Try this to avoid that error

    Code (csharp):
    1.  
    2.  
    3. newPlayer = Instantiate(player, SpawnPoint[S].position, SpawnPoint[S].rotation);
    4.    
    5.  
    6.  
     
  9. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    code so far
    Code (CSharp):
    1. public int S;
    2.  
    3.     public Transform[] SpawnPoint;
    4.  
    5.     public Button[] Spawner;
    6.  
    7.     public GameObject Spawners;
    8.  
    9.     public GameObject player;
    10.  
    11.     void Awake()
    12.     {
    13.         SetSpawnInfo();
    14.     }
    15.    
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonUp(0))
    19.             {
    20.             //place here
    21.                 for (int I = 0; I < Spawner.Length; I++)
    22.                 {
    23.                 if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    24.                     Camera.main))
    25.                     {
    26.                         S = I;
    27.                         SpawnPlayer(player);
    28.                     }
    29.                 }
    30.             }
    31.     }
    32.  
    33.     void SetSpawnInfo()
    34.     {
    35.         SpawnPoint = this.GetComponentsInChildren<Transform>();
    36.         Spawner = Spawners.GetComponentsInChildren<Button>();
    37.     }
    38.  
    39.     public void SpawnPlayer(GameObject newPlayer)
    40.     {
    41.         if (newPlayer != null)
    42.         {
    43.             Destroy(newPlayer);
    44.         }
    45.  
    46.         newPlayer = player;
    47.  
    48.         if (newPlayer != null)
    49.         {
    50.             newPlayer = Instantiate(player, SpawnPoint[S].position, SpawnPoint[S].rotation);
    51.         }
    52.  
    53.        
    54.     }
    still have this error
    upload_2020-11-3_23-33-22.png

    but spawning is taking place
    upload_2020-11-3_23-34-10.png
     
  10. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Hmm that should be setting newPlayer as the clone so it would be able to be destroyed.

    In function SpawnPlayer(GameObject newPlayer)

    remove Gameobject newPlayer and inside the function, before the first if statement

    add
    Code (csharp):
    1.  
    2.  
    3. Gameobject newPlayer;
    4.  
    5.  
    and on line 27 remove “player”

    so that its only SpawnPlayer();
     
  11. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    done... an error below
     
  12. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    done...an error below
    upload_2020-11-3_23-57-41.png
     
  13. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    GameObject newPlayer = new GameObject();
     
  14. CF_Sixdotsstudios

    CF_Sixdotsstudios

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

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    ran the code...multiple clones in the scene, the errors are all gone though
    upload_2020-11-4_0-5-39.png

    upload_2020-11-4_0-5-15.png
     
  16. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay just remove what you just added but instead at the beginning of your script just make

    Code (csharp):
    1.  
    2. public GameObject newPlayer;
    3.  
     
  17. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    d
    done
     
  18. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    what will go in that exposed variable?
     
  19. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Just leave it blank. When the player spawns it should end up being the clone that fills it
     
  20. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool.. let try and run it
     
  21. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    ur the man Boo-Let....RESULT!!!!
     
  22. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
  23. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Lol good! Sorry for the long run around. I usually implement it differently and that method is different than the approach here so I was having to remember the order of things... plus driving lol makes coding on a phone difficult
     
  24. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    except button one keeps spawning in empty space...any way to correct that?
     
  25. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    lol...no worries. i take the time to study the code. i was getting worried u were tiring...i need the code for a scene am making where the player will spawn at specific points like fast travel to aid scene exploration
     
  26. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    this what i mean by spawning in space
    upload_2020-11-4_0-22-59.png
     
  27. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Yeah there’sa way to fix it I was going to wait until the main issue was working.

    so a really quick fix is to change

    Code (csharp):
    1.  
    2. S = I;
    3.  
    to

    Code (csharp):
    1.  
    2. S = I + 1;
    3.  
     
  28. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool... let me try that...
     
  29. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Isn’t that empty space the location of the parent object holding your spawn points?
     
  30. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    Boo-Let ur the Man!!! All the buttons are working... cant thank you enough... do u have a channeli can subscribe to or a patron i can join lol...u've helped me a tonne
    upload_2020-11-4_0-46-7.png

    upload_2020-11-4_0-46-28.png

    upload_2020-11-4_0-46-47.png

    upload_2020-11-4_0-47-1.png
     
  31. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    lol no I don’t but feel free to message me on here anytime. If I know how to do what you need I’ll be glad to help out. And it won’t take this long again! Maybe next time I’ll be at a computer lol
     
  32. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    lol... thanks again. i'll certainly call i really appreciate u taking the time. will certainly be getting in touch when i get stuck...hopefully someday soon with ur grit i can to be as good...:):D
     
  33. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    by the way i dont car how long it takes cause the reward out ways the hustle...hehehehehe...next time less keep texting and driving to a minimum...:D
     
  34. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Haha I’ll keep that in mind!! Anytime buddy!!
     
  35. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    thanks again...