Search Unity

Only assignment, call, increment, decrement, await, and new object expressions can be used as a

Discussion in 'Scripting' started by Ali_Fuat_ADAK1, Jan 15, 2022.

  1. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    I was working on my coin script but it always says "
    Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
    " I couldnt find a solution pls help

    My Coin Script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class CoinPicker : MonoBehaviour
    {

    private float coin = 0;
    public TextMeshProUGUI textCoins;


    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.transform.tag == "BronzeCoin")
    {
    coin + 1;
    textCoins.text = coin.ToString();
    Destroy(other.gameObject);
    }else if (other.transform.tag == "SilverCoin")
    {
    coin + 2;
    textCoins.text = coin.ToString();
    Destroy(other.gameObject);
    }else if (other.transform.tag == "GoldCoin")
    {
    coin + 5;
    textCoins.text = coin.ToString();
    Destroy(other.gameObject);
    }
    }

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Dude, I just explained ALL of this to you yesterday:

    https://forum.unity.com/threads/the-type-or-namespace-cannot-be-found.1225035/#post-7810092

    Your typing accuracy is still far below where it needs to be.

    coin + 1;


    is NEVER going to be valid standing alone like that.

    I'll post it again: this part is NOT OPTIONAL:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.


    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
    Bunny83 likes this.
  3. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    I am following a tutorial but i dont still know how it is gonna valid (sorry for bad english)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    The problem is you are NOT following a tutorial. I don't care what tutorial it is, but it NEVER told you to type this:

    Code (csharp):
    1. coin + 1;
    I have no idea what it told you to type, but I guarantee that it absolutely did NOT tell you to do that.

    Go back and see what it actually told you to do.
     
  5. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    Yes it didnt say it but i have multiple coins and i want them to have diffrent values and there is no tutorial for this
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Are you trying to increment coins by 1?

    The correct syntax is:

    Code (csharp):
    1. coin += 1;
    You probably want to work through a LOT of other tutorials first before you start just randomly trying code syntax, or else you are going to have a VERY hard time doing this.

    Here are some great starting tutorials:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:

     
  7. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    thanks bro i will watch them all
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Remember: watching tutorials is a complete waste of time.

    Make sure you do Step #2 in my list above: fully understand each part of the tutorial until you can explain it to your dog or houseplant.

    If you choose to watch a tutorial and you do NOT do Step #2, you have just wasted your time.
     
    Bunny83 likes this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    I know this is a me thing... and probably just comes from the fact that when I learned to program "tutorials" weren't really a thing. And even if they were, they certainly were NOT video tutorials.

    I mean I guess in theory VHS tutorials for some things probably existed. I do remember VHS videos of how to science labs existing. So like... would you consider those video tutorials?

    But yeah... I just don't get tutorials.

    To each there own, if y'all like them, have at it.

    I just don't get it. Especially the video ones. Video tutorials are those things that when I'm googling some specific question (something likely would show up in a stackoverflow thread or something), I'll trip over a video tutorial. And I'll click on it after exhausting all my other options and every single time I get angry... I can't highlight text, I have to sit here and watch some guy type slow as dirt, odds are it's some person I can't understand because their mic is underwater or they speak with a pitch only dogs can hear. (oh, and don't get me on the robot voice ones... ugh. If I wanted to be condescended to by a robot I'd go hop on Discovery One and hang with HAL)

    And I just die a little inside.

    Like... I speed read when I research. I'm looking for key points in the article/tutorial/documentation, and then slow down if I've determined the information to be close to what I'm actually looking for (since you know... you don't usually find sources solving your problem 100%, it's only tangentially related). And then like I can copy and paste some code from it and play around with it in a demo project (I always have one open just for this purpose, even just a simple Console application in Visual Studio. In enterprise land I fell in love with LinqPad for database related workshopping).

    But I can't do that with a video!

    And I just find myself jumping around the video hoping they say something of relevance. But I likely skipped over it because the video is 50 mins long and a mere 5 pixel scroll is like 4 minutes of video.

    ...

    I don't get it.

    Again, to each their own, y'all might have way better results with that stuff than me.

    But I just get this sneaking feeling every time I read a "I'm following a video tutorial" post that they are subconsciously having these same impatient issues I have that I personally think are inherent to the medium.
     
    Bunny83 and Kurt-Dekker like this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    And I have asked you previously, use code-tags from now on. No more huge posts of plain text please.
     
    Bunny83 likes this.
  11. zacharyaydenjohn

    zacharyaydenjohn

    Joined:
    Jul 3, 2022
    Posts:
    5
    Same thing happened to me
    script:
    using UnityEngine;

    public class FollowPlayer1 : MonoBehaviour {

    public Transform player;
    public Vector3 offset;

    // Update is called once per frame
    void Update () {
    transform.position = player.position; + offset;
    }
    }
     
  12. zacharyaydenjohn

    zacharyaydenjohn

    Joined:
    Jul 3, 2022
    Posts:
    5
    any advice?
     
  13. zacharyaydenjohn

    zacharyaydenjohn

    Joined:
    Jul 3, 2022
    Posts:
    5
    I am very new with unity
     
  14. zacharyaydenjohn

    zacharyaydenjohn

    Joined:
    Jul 3, 2022
    Posts:
    5
    it also says operator + cannot be used with vector3
     
  15. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    NOTHING "happened to" you. You made typos, then you necro-posted to an ancient thread for your own typos, and on top of it you failed to format your code properly so nobody can even tell you what your error is!!

    May I suggest instead you just fix your own typos? It's only ... professional.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors... see the top of this post!
     
  17. zacharyaydenjohn

    zacharyaydenjohn

    Joined:
    Jul 3, 2022
    Posts:
    5