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

Resolved My platforming needs some help :)

Discussion in 'Scripting' started by Jinngineer, Jul 19, 2020.

  1. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello, Its me again.

    Ive Been trying to instatiate a platform at a random distance and height. Then when i leave the platform i want it to get destroyed. Now in general what i have works. But i keep getting an error that i cant destroy a prefab. So i looked online and was told that i have to save the prefab in another variable. So i think i did that. But the error persists.

    Also i am trying to get the trigger to only activate once and spawn just one platform. And again the internet came to my rescue with the info that i need to use a bool and have it set to active once you enter the trigger. Which i also think i did, but it doesnt seem to work so most likely i have a mistake somewhere.

    Finally, for some reason my movement has become very sluggish. I need to keep uping the force applied to the object for some reason. I dont see how this script would interfere with the movement script as that is on a different C# page.

    As a result of all this i kinda got lost. And now im not 100% sure what to cut, trim ot outight rewrite when it comes to this script.

    Any pointers would be helpful.

    Code (CSharp):
    1.  
    2.  
    3. public class SpawnPlatform : MonoBehaviour
    4. {
    5.     public GameObject platform;
    6.     float Randx, Randy;
    7.     Vector3 pos;
    8.     GameObject clonePlatform;
    9.     bool hasBeenTriggered = false;
    10.  
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         Randx = Random.Range(3, 6);
    16.         Randy = Random.Range(-3, 1);
    17.         pos = transform.position;
    18.  
    19.         clonePlatform = platform;
    20.     }
    21.  
    22.      void OnTriggerEnter(Collider other)
    23.     {
    24.         hasBeenTriggered = true;
    25.         spawnNextPlatform();
    26.  
    27.      
    28.     }
    29.     void OnTriggerExit(Collider other)
    30.     {
    31.         Destroy(this.clonePlatform);
    32.     }
    33.  
    34.  
    35.  
    36.     void spawnNextPlatform()
    37.     {
    38.         if (hasBeenTriggered)
    39.         {
    40.             Vector3 Newpos = pos + new Vector3(Randx, Randy, 0);
    41.             Instantiate(clonePlatform, Newpos, Quaternion.identity);
    42.         }
    43.        
    44.     }
    45.  
    46. }
    47.  
    48.  
    Best Regards :)

    p.s. hope my code tags worked xD
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    For one why would you do this
    Code (CSharp):
    1. clonePlatform = platform;
    every update? Seems like it should be on Start().

    Why are you defining pos in Update instead of Start()? I think you mean to do that in spawnNextPlatform().

    You are Instantiating a new one to spawn the next; why is there a cloneplatform at all?
     
  3. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello, thanks for the quick reply :)

    1. I didnt realize that i put cloneplatform=platform in update xD

    2. Defining pos in update because i didnt know where else to put it, putting it in spawnNextPlatform() seems reasonable.

    3. I added a clonepaltform cause i keep having an error that i cant destroy a prefab... And while searching online i found that that might be a solution.
     
  4. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Specifically i get :

    Destroying assets is not permitted to avoid data loss.
    If you really want to remove an asset use DestroyImmediate (theObject, true);

    and:

    ArgumentException: The Object you want to instantiate is null.

    as errors
     
  5. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Hi :) The reason you are getting this error is as it says - you are trying to Destroy() a prefab, not an instance of an asset. An analogy to what you're doing is a bit like trying to uninstall a program, instead of going into task manager and ending the process.

    When you create your platform, you need to save a reference to it. So do:

    Code (CSharp):
    1. GameObject myNewPlatform = Instantiate(clonePlatform, Newpos, Quaternion.identity);
    Then when you want to destroy it, do:

    Code (CSharp):
    1. Destroy(myNewPlatform);
    Obviously myNewPlatform will have to be a global variable probably so it can be accessed in multiple functions, but you get the idea.
     
  6. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    @Anthony Sharp,

    Thanks for the assist.The errors have disapeared and now only one object is being spawned per triggerEnter... But none of the platforms are being destroyed

    Code (CSharp):
    1.  
    2.  
    3.     public GameObject platform;
    4.     int Randx, Randy;
    5.     Vector3 pos;
    6.     Vector3 Newpos;
    7.  
    8.     bool hasBeenTriggered = false;
    9.  
    10.     public GameObject myNewPlatform;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.      
    16.     }
    17.     void Update()
    18.     {
    19.         Randx = Random.Range(3, 6);
    20.         Randy = Random.Range(-5, 1);
    21.         pos = transform.position;
    22.         Newpos = pos + new Vector3(Randx, Randy, 0);
    23.  
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.      
    29.             spawnNextPlatform();
    30.      
    31.      
    32.  
    33.  
    34.     }
    35.  
    36.     public void spawnNextPlatform()
    37.     {
    38.         if (!hasBeenTriggered)
    39.         {
    40.          GameObject myNewPlatform = Instantiate(platform, Newpos, Quaternion.identity);
    41.         hasBeenTriggered = true;
    42.         }
    43.  
    44.      
    45.     }
    46.  
    47.     private void OnTriggerExit(Collider other)
    48.     {
    49.         Destroy(myNewPlatform);
    50.     }
    51.  
    52.  
    53. }
    54.  
    55.  
    this is what i think you meant. Ive also tinkered with usingf Destroy(gameObject) but that didnt do anything.

    Best Regards,

    p.s. would there be any reason why using this script results in me needing to use almost 500 force to move my object when before adding this script i only needed 15?
     
  7. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    The reason it's not being destroyed is because you are creating two versions of myNewPlatform. First you do this:

    Code (CSharp):
    1. public GameObject myNewPlatform;
    Which is good. But then you also do:

    Code (CSharp):
    1. GameObject myNewPlatform = Instantiate(platform, Newpos, Quaternion.identity);
    In that function, you're essentially re-creating a function-specific version of myNewPlatform instead of using the global myNewPlatform you created before. So change that line to:

    Code (CSharp):
    1. myNewPlatform = Instantiate(platform, Newpos, Quaternion.identity);
    I wish I could tell you :p
     
  8. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    hmmmm. I think there might be a slight misunderstanding xD.

    What i am trying to do is create a endless platformer. To do this i created a prefab of a platform that has a trigger attached. OnTriggerEnter() the next platform is to be created and upon OnTriggerExit() i want to delete the platform im jumping from.

    What we just did was make the platform that we spawned to disapear during the jump as i leave the trigger xD
     
  9. Andrew-Carvalho

    Andrew-Carvalho

    Joined:
    Apr 22, 2013
    Posts:
    39
    @AnthonySharp is correct, but I'll try to explain it with an example.


    Code (CSharp):
    1. class SomeClass
    2. {
    3.     private int myInt = 5;
    4.  
    5.     public void Foo()
    6.     {
    7.         int myInt = 0;
    8.  
    9.         Debug.Log(this.myInt);        // Prints out 5
    10.         Debug.Log(myInt);                // Prints out 0
    11.     }
    12. }
    If you declare a variable, (by putting a type before, like
    int myInt;
    then you are defining a *new* variable. You are allowed to reuse variable names within different scopes (more or less....) so what you have in the above code is a variable named myInt that is in the scope of the instance of the class, defined right below the class definition. You also have a variable named myInt that is defines in the scope of the Foo function, which is allowed.

    If you use myInt, C# will assume you want the most specific scope. Inside Foo, the most specific scope is the function, so it will reference the myInt defines *inside* the funciton. You'll notice in my Debug.Log I used the
    this
    keyword, which means I want to use the *class* scopes myInt. In general, it's not great practice to reuse variable names because it just creates a lot of confusion when reading code.

    In your case, I think you may be adding the GameObject before the variable name accidentally, or from a misunderstanding of variable scopes and accessing/assigning vs defining. If you remove the
    GameObject
    from before your myNewPlatform variable inside the function, that should fix your problem.
     
  10. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Exactly.

    Okay I see what you're trying to do .... In that case, do this:

    Code (CSharp):
    1. private void OnTriggerExit(Collider other)
    2. {
    3.      Destroy(gameObject);
    4. }
    That will cause the platform to destroy itself, rather than destroy the new platform. :cool::cool::cool::cool::cool:

    EDIT: you don't actually need to keep a reference to your new platform then (myNewPlatform). You can chuck that out if you like.
     
  11. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello, Thanks for all the help.
    I got it to work due to alll of you :)

    @AnthonySharp.. needed to use Destroy(transform.parent.gameObject);
    Destroy(gameObject) only destroyed trigger :p

    Alright, now back to work.

    Later, and thanks for the help :D
     
    AnthonySharp likes this.