8 errors (new to C# coding) on Destroy on MouseDown

Help please!
I know only a bit of C#
And of course, I am using C# and its a 2D project

using UnityEngine;
using System.Collections;

public class Destruction : MonoBehaviour {
    var Destoyed = int;
   
    // Update is called once per frame
    void OnMouseDown (GameObject); {
        Destroy (GameObject); {
            RaycastHit2D;
    }
   
}
  • In line 5… try “int destroyed” instead. Also, you could just delete the line, because I don’t see you doing anything with that variable, anyway.
  • In line 8, remove the “GameObject” inside the () brackets. OnMouseDown doesn’t need a parameter
  • In line 8, remove the semicolon. It’s not a statement, it’s a function declaration. Those don’t have semicolons after them.
  • In line 9, why do you open a { bracket? Remove that.
  • In line 9, You are trying to destroy the class “GameObject”, which doesn’t work. You probably want to destroy “gameObject”, which is the GameObject this script is attached to. Lower/Upper case matters in C#!
  • In line 10, WTF. Just remove it entirely.

Var is not for C# but java so it would go

int Destryoed;
//or if you want add value at start
int Destroyed = 0;
//but it seems that you are checking if something is destroyed? so better to use boolean
bool Destroyed = false;

And the errors are?

JavaScript is not Java! But yeah, you probably want a boolean.

And while we are getting technical, C# does support var, but only at local scope, not at the class scope.

Now I’ve gone and confused poor @Findev . It’s probably worth you ignoring this post for now and pretending var doesn’t exist.

2 Likes

As a matter of fact, it would be best for all of humanity to pretend var didn’t exist.

I know but its much easier, isn’t it called UnityScript also?

I wish. Unity keeps marketing UnityScript as JavaScript, which is a) wrong, b) confusing and c) wrong. It’s a JavaScript dialect at best.

1 Like

It’s useful for working with third party code if you don’t want to look up the return type of a method. That’s mostly syntatic sugar, but sometimes I get lazy. It can also insulate your code a little bit against implementation changes.

But yeah, on the while it wouldn’t be a major loss if the keyword disappeared from C#. And for UnityScript users it would be a massive boon.

Let’s be honest. It’s C# rearranged slightly to look like JavaScript. It has pretty much none of the conventions of regular JavaScript.

1 Like

I think I got this. Thanks everyone!

Actually, when I tried your solution @Taschenschieber I have 3 more errors, which 5 have gone.

ERRORS:
(7,30): Unexpected symbol ‘{’ in class, struct, or interface member declaration
(8,26): Identifier expected
(9,1) Unexpected symbol ‘}’ in class, struct, or interface member declaration

using UnityEngine;
using System.Collections;

public class Destruction : MonoBehaviour {
   
    // Update is called once per frame
    void OnMouseDown (); {
        Destroy (gameObject)
}

Remove ; after OnMouseDown() and add it after (gameobject)

using UnityEngine;
using System.Collections;
public class Destruction : MonoBehaviour {
  
    // Update is called once per frame
    void OnMouseDown (){
        Destroy (gameObject);
    }
}

This is how it should look. Go look through some simple unity tutorials to get a feel for the format of C#

@phoda and @RiokuTheSlayer Thanks for your solution!

Actually, there is another error that was not killed with the poison yet. :rage:
(9,1): error CS8025: Parsing error

using UnityEngine;
using System.Collections;

public class Destruction : MonoBehaviour {
   
    // Update is called once per frame
    void OnMouseDown () {
        Destroy (gameObject);
}
using UnityEngine;
using System.Collections;
public class Destruction : MonoBehaviour {
    // Update is called once per frame
    void OnMouseDown (){
        Destroy (gameObject);
    }
}

Again, this is how it should look. You’re missing the } on line 9, then another on line 10

Thanks a lot @RiokuTheSlayer ! The script works! :smile: