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
    we have the following error
    upload_2020-11-3_20-11-7.png
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    I had to edit the above post... I placed something on wrong position but NOW it’s right. So redo the above line..

    my mad man I’m really not focusing as I’m doing this lol
     
  3. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    its
    its all good...am learning...
     
  4. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    lol yeah but if I was actually sitting down and thinking I would of had it right from the get go. Where I’m constantly looking up and down to pay attention to the road I’m losing thought with the code
     
  5. 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.     }
     
  6. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    this is the code so far...unless i didnt edit it right...we have the same Log
    upload_2020-11-3_20-22-22.png
     
  7. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    actually worried about u driving and doing this lol...be careful...lol
     
  8. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Create this

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.       if (Input.GetMouseButtonUp(0))
    5.             {
    6.                //place here
    7.             }
    8. }
    9.  
    10.  
    and put

    Code (csharp):
    1.  
    2. for (int I = 0; I < Spawner.Length; I++)
    3. {
    4. if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition, Camera.main)
    5. {
    6.  
    7. Debug.log(Spawner[I]);
    8.  
    9. }
    10. }
    11.  
    12.  



    Where it says “place here”.

    before running, remove the functions from your button set up so that no script is attached to the button

    Attach the script to your spawn manager game object
     
    Last edited: Nov 3, 2020
  9. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Also

    Code (csharp):
    1.  
    2.  
    3. void OnEnable()
    4. {
    5.       SetSpawnInfo()
    6. }
    7.  
    place this above SetSpawnInfo
     
  10. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    COOL
    minor errors..
    upload_2020-11-3_20-43-31.png

    code so far

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

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    You missed a section. Look above and you’ll see the for loop in update. Paste it into script like above
     
  12. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool..i knew i got confused somewhere
     
  13. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    I've done the above...what's next. play the scene?
     
  14. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    All errors gone?
     
  15. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    Yes... all gone
     
  16. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Now make sure script is attached to your Spawnmanager gameobject and hit play and click a button and see if it logs a different number in the console Also make sure to add this above Update()

    Code (csharp):
    1.  
    2. void Awake()
    3. {
    4.       SetSpawnInfo();
    5. }
     
  17. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    cool...on it
     
  18. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    result...here's the latest log as i press each button respectively
    upload_2020-11-3_21-13-34.png
     
  19. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay now make a
    Code (csharp):
    1.  
    2. public int S;
    3.  
    now in update, instead of debug.log. Replace it with

    Code (csharp):
    1.  
    2. S = I;
    3. SpawnPlayer(player);
    4.  
    And then change spawnplayer to

    Code (csharp):
    1.  
    2. public void SpawnPlayer(GmaeObject newPlayer)
    3. {
    4.       if (newPlayer != null)
    5.       {
    6.           Destroy(newPlayer);
    7.       }
    8.  
    9.       newPlayer = player;
    10.  
    11.       if (newPlayer != null)
    12.       {
    13.             newPlayer = Instantiate(newPlayer,
    14.             SpawnPoint[S].position,
    15.             SpawnPoint[S].rotation);
    16.        }
     
  20. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    Cool... On it...
     
  21. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Be sure to use updated post.. I had to edit a lot to remove unwanted characters from pasting
     
  22. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    All done and no errors...
     
  23. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Well click play and see if it works lol you don’t need to add anything to button functions either
     
  24. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    wow...that's cool. let me try it...
     
  25. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    he
    hey. at least there's no errors. the cylinder doesnt appear on the spawnpoint on play after clicking button 1
    upload_2020-11-3_21-50-9.png

    buttons 2 to 4 the cylinder spawns on the spawn location on playing the scene. the other buttons dont work as desired when u click them as before without stop and playing the scene for each respectively.
    upload_2020-11-3_21-52-6.png

    below is the scene setup so far
    upload_2020-11-3_21-55-38.png

    the log so far when i press any button...

    and the 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.     void OnEnable()
    16.     {
    17.         SetSpawnInfo();
    18.     }
    19.     void Update()
    20.     {
    21.         if (Input.GetMouseButtonUp(0))
    22.             {
    23.             //place here
    24.                 for (int I = 0; I < Spawner.Length; I++)
    25.                 {
    26.                 if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    27.                     Camera.main))
    28.                     {
    29.                         S = I;
    30.                         SpawnPlayer(player);
    31.                     }
    32.                 }
    33.             }
    34.     }
    35.  
    36.     void SetSpawnInfo()
    37.     {
    38.         SpawnPoint = this.GetComponentsInChildren<Transform>();
    39.         Spawner = Spawners.GetComponentsInChildren<Button>();
    40.     }
    41.  
    42.     void ButtonPress()
    43.     {
    44.         SpawnPlayer(player);
    45.     }
    46.  
    47.     public void SpawnPlayer(GameObject newPlayer)
    48.     {
    49.         if (newPlayer != null)
    50.         {
    51.             Destroy(newPlayer);
    52.         }
    53.  
    54.         newPlayer = player;
    55.  
    56.         if (newPlayer != null)
    57.         {
    58.             newPlayer = Instantiate(newPlayer,
    59.             SpawnPoint[S].position,
    60.             SpawnPoint[S].rotation);
    61.         }
    62.  
    63.         for (int I = 0; I < Spawner.Length; I++)
    64.         {
    65.             if (RectTransformUtility.RectangleContainsScreenPoint(Spawner[I].transform as RectTransform, Input.mousePosition,
    66.                 Camera.main))
    67.             {
    68.                 if (newPlayer != null)
    69.                 {
    70.                     Destroy(newPlayer);
    71.                 }
    72.  
    73.                 newPlayer = player;
    74.  
    75.                 if (newPlayer != null)
    76.                 {
    77.                     newPlayer = Instantiate(newPlayer, SpawnPoint[I].position, SpawnPoint[I].rotation);
    78.                     Debug.Log(I);
    79.                     Debug.Log(Spawner);
    80.                 }
    81.             }
    82.  
    83.  
    84.         }
    85.     }

    maybe am just setting up the scene wrong
     
  26. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    the nu
    the numbers in the public variable int 'S' and changing based on the button press though
     
  27. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Oh delete the OnEnable function. I wanted to to use that instead of awake in case you ever set the spawn manager inactive. So delete OnEnable function and in SpawnPlayer function delete lines 63-81 that shouldn’t be there either.
     
  28. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Also your spawnpoints array is adding the parents transform so that’s giving you an extra (and off position) spawn point which will throw it off
     
  29. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Also you can delete ButtonPress function. That’s no longer needed
     
  30. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    c
    cool...
     
  31. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    ah i see
     
  32. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    After making those changes to the functions try it. See if it reacts how it should other than spawning the player one spawn point off
     
  33. 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.     }
    code so far. same thing as before. only move to the button i press first then it doesnt move at all unless on play i press another button to which it move only the once respectively
     
  34. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    let rethink this. maybe i can jus move the cylinder instead on spawning. just make it move from spawn point to spawn point adopting the transform position and transform rotation. cause all need is my character to move to a particular location on command.

    below is the code snipet i used for player movement but the problem is the player doesnt move to the right location after several location button move request commands. this commands was excecuted on button press. there's about 11 location in my scene the player need to move to on command...

    Code (CSharp):
    1. public GameObject player;
    2.     public GameObject location;
    3.          
    4.     public void MoveToLocation()
    5.     {
    6.         player.transform.position = location.transform.position;
    7.         player.transform.rotation = location.transform.rotation;
    8.     }
     
  35. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    hey i did but its still only respond to only the first button press after playing the scene
     
  36. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    So if you click button one, player spawns.. but if you click button 2 or 3 it does nothing unless you exit play mode.. hit play, then click button 3 it’ll spawn player 3?
     
  37. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    yes...thats what its doing. it only moves to the respective button only when i stop and play the scene if press the other buttons without doing that nothing happens
     
  38. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    When you try and do it on the scene is the Console displaying any errors
     
  39. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    there's no errors at all...
     
  40. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Are all the OnClick functions removed from every button?
     
  41. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    yes... all of them
    upload_2020-11-3_22-37-8.png
     
  42. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Hmm... so when you’re in play mode and you spawn one and then hit another button is “s” value changing?
     
  43. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    yes the s value us changing...but there's no movement of the cylinder
     
  44. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Are the SpawnPoint positions in the array still active when you change?
     
  45. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    the spawn point are still there though the player varriable is empty after several button presses as seen below
    upload_2020-11-3_22-47-41.png
     
  46. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Is there only one instance of this script attached to that game object? And isn’t attached to any other? That’s actually really weird.. I’ve never had or seen that happen
     
  47. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    l
    let me check.
     
  48. CF_Sixdotsstudios

    CF_Sixdotsstudios

    Joined:
    Apr 10, 2020
    Posts:
    92
    i
    i've checked that the only thing its attached to
     
  49. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Post your code again please
     
  50. CF_Sixdotsstudios

    CF_Sixdotsstudios

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