Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DestroyObject

Discussion in 'Scripting' started by ArrayCS, Sep 20, 2017.

  1. ArrayCS

    ArrayCS

    Joined:
    Sep 18, 2017
    Posts:
    29
    So I've made a jumpscare in my horror game and it jumps out right in front of you. BUT after the jumpscare there is just an invisible barrier stopping me from walking (where the jumpscare was) i've made another post and somebody said you can use destroyobject. so i've made this small script and im getting errors with it. it basically just destroys the object after two seconds, or it should.

    using UnityEngine;

    public class ScriptExample : MonoBehaviour

    void DestroyObjectDelayed();
    {
    // Kills the game object in 5 seconds after loading the object //
    Destroy(gameObject, 2);
    }

    Errors in order

    Assets/Scripts/Destroy.js(1,6): UCE0001: ';' expected. Insert a semicolon at the end.

    Assets/Scripts/Destroy.js(3,28): BCE0043: Unexpected token: :.

    Assets/Scripts/Destroy.js(3,43): UCE0001: ';' expected. Insert a semicolon at the end.

    Assets/Scripts/Destroy.js(5,9): UCE0001: ';' expected. Insert a semicolon at the end.

    Please help i really need this to work
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
  3. ArrayCS

    ArrayCS

    Joined:
    Sep 18, 2017
    Posts:
    29
    Hey sorry to be a pain but do you think that you could maybe write this script without the syntax errors? im pretty new to coding and i just need this script to delete the object after 2 seconds. @Brathnann
     
  4. ArrayCS

    ArrayCS

    Joined:
    Sep 18, 2017
    Posts:
    29
    using UnityEngine;

    public class ScriptExample : MonoBehaviour{}

    void DestroyObjectDelayed()
    {
    // Kills the game object in 5 seconds after loading the object //
    Destroy(gameObject, 1);
    }

    thats the new scripts with only 2 errors i will post the errors

    Assets/Scripts/Destroy.js(1,6): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Scripts/Destroy.js(3,28): BCE0043: Unexpected token: :.
    @Brathnann
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Please do listen when I ask you to use code tags. Also, if you have other scripts, you should be able to compare and see what doesn't look right and you can see your curly brackets aren't right. I have fixed it for you.

    Code (CSharp):
    1. public class ScriptExample : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         DestroyObjectDelayed();
    6.     }
    7.  
    8.     void DestroyObjectDelayed()
    9.     {
    10.         // Kills the game object in 2 seconds after loading the object
    11.         Destroy(gameObject, 2);
    12.     }
    13. }
    However, looking at this you're not actually calling the DestroyObjectDelayed method.
     
    Last edited: Sep 20, 2017
  6. ArrayCS

    ArrayCS

    Joined:
    Sep 18, 2017
    Posts:
    29
    Thanks but theres still one error. also i dont really understand the code tags.. i've pasted the code into it but im still having this error...
    Assets/Scripts/Destroy.js(1,28): BCE0043: Unexpected token: :.
    @Brathnann
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I just noticed your file extension is .js
    You need to make a csharp script, not a javascript script.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You should really try learning C# and going through some of the tutorials here for scripting, because you will get stumped a lot otherwise.
    If that's all that script does, you can even shorten it to:
    Code (csharp):
    1.  
    2. void Start(){
    3.   Destroy(gameObject, 2);
    4.  }
    5.  
    :)
     
    Ryiah likes this.