Search Unity

Can someone tell me how to fix this error? Beginner

Discussion in 'Scripting' started by Next2Stupid1, Jun 27, 2019.

  1. Next2Stupid1

    Next2Stupid1

    Joined:
    Jun 26, 2019
    Posts:
    1
  2. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    If you click on the errors it'll go to the line they're in. As you can see, the problem is in line 15, and it has a red mark blow the ":". You probably used to write code in javascript and use the ":" to tell unity what's the class of the object. In c# you don't decleare the class like this:
    other : Collider;
    instead you put the class first an then the name you want to use, like this:
    Collider other;
    Also, you don't use the keyword "function" you just put the return type, that is the class you want to return. For example, if your function is an adition and it returns a number, you use "int".
    So it'll be like:

    Code (CSharp):
    1.  
    2. int firstNum = 3; //you put the class first
    3. int secondNum = 4; //you put the class first
    4.  
    5. //"public" as in other scripts can use it.
    6. //"int" because this function returns an int
    7. //"MyAddition" is the name, you can choose whatever
    8. //"(int a," is the first parameter, as you can see the class is expressed first, then the name
    9. //"int b)" is the second parameter, again the class is expressed first, then the name
    10. public int MyAddition(int a, int b)
    11. {
    12. return a + b; //as you can see it returns an int
    13. }
    If your function doesn't return a thing, you just use "void". That's what you should have used here.
    So, to sum it up, you change the line 15 to this:
    Code (CSharp):
    1. void OnTriggerEnter (Collider other){
    2.  
     
    Last edited: Jun 27, 2019
    Next2Stupid1 and Lethn like this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Lethn likes this.
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    First off, use the code tags, it makes everything much easier to read and deal with, you can just copy and paste it into the forum post. This looks a lot like Javascript so no wonder you're having problems, Unity has made Javascript obsolete so if you're looking at old tutorials that will no longer work, make sure to look for C# tutorials only.

    Try this

    Code (CSharp):
    1. Private Void Function OnTriggerEnter (other collider )
    2.  
    3. {
    4. if (other.tag == "coin"
    5.  
    6. {
    7.    score += 5;
    8.    Destroy (other.gameobject);
    9. }
    10.  
    11. }
    12.  
    I haven't tested this code and only done a quick write up, let me know if you have problems, you need to also put the score up at the top as an public integer and then it should all work fine, that being said I think they put in recently a way for you to write shorthand so experiment and see if it works regardless.

    I think you're looking at some very outdated tutorials there.

    Edit: LOL I had just finished my post and of course two others got there before me :p I suspect just checking out the up to date tutorial link will be easiest.
     
    Next2Stupid1 likes this.