Accidentally using variable names that coincide with dot operators

So I was having trouble getting the score to go up despite following what everything Rick was doing. I realized after 2 hours of troubleshooting that it was because of my use of the word “clip” as my variable in CoinPickup.cs.

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

public class CoinPickup : MonoBehaviour
{

    [SerializeField] AudioClip clip;
    [SerializeField] int pointsForCoinPickup = 100;
    bool wasCollected = false;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player" && !wasCollected)
        {
            wasCollected = true;
            FindObjectOfType<GameSession>().AddToScore(pointsForCoinPickup);
            AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
            Destroy(gameObject);
            
        }
    }
}

My questions are:

  1. When creating a variable name, how do I check that there aren’t any operators or method names associated with the variable I’m creating so that I do not make the same mistake again?
  2. I understand that I’ve made a mistake (and the code worked when I fixed it to using variable name “coinPickupSFX” like Rick did), but this is a variable relating to sound. How does this hamper counting & collection of score in any way?? In my code the line "AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position); " is AFTER the FindObjectOfType code so I don’t see how it could impact the code.

The compiler will typically give you an error or a warning if it’s an inherited and suggest that you use new or override.

Like @MichaelP said, the editor will highlight the issue

I don’t understand this ‘mistake’ you made because there is no clip on a MonoBehaviour (or any of it’s parent types) that I can see. Why this broke the anything, and why changing the name to something else fixed it is a mystery to me…


@MichaelP I went back to check to see if there were error messages when I used the “clip” variable again, but I didn’t see any issues. This is probably partly why I made the mistake. I might be talking about edge cases here, but I just want to learn from every severe mistakes I’ve made so I don’t waste time in the future. (I am scared that I have more cases of this in the future and lose significant time trying to debug other projects later down the road if I don’t understand the root cause now.) Is my intellisense not working properly, perhaps?


@bixarrio This is the error message given when I used the variable “clip” instead of “coinPickupSFX”. It seems so strange reading the error code since I declared the variable literally the first line of my class. hahah

What you were describing is a syntax error. The IDE picks those up for you and helps you.

The one’s that are going to bake your noodle are the logic errors. They’re the ones that are going to make you pull your hair out. They’re the ones that cost you days. Sometimes because you’ve missed a {} or } in the wrong place. Others from a case of a brain fart.

It’s all part of the journey.

This can be a chicken or the egg issue. Are you having troubles because there’s something you’re doing wrong, or are you doing something wrong because you haven’t let your learning go deep enough so your programming skills have advanced beyond that issue.

I don’t have the answer. Both schools are right and wrong at the same time. Go deep or go wide. I think somewhere in the middle is best, but in the end, going deep is preferable.

1 Like

This error is because you are trying to play a clip that has not been assigned. If you check the inspector, you may find that there is nothing in the clip field

1 Like

@MichaelP thank you for your reply. I’ll keep the logic issues as I learn and try to keep everything as consistent as possible.

@bixarrio LOL
Yeah you were right. Beginner’s mistake. I put something in there and I realized I have issues with score once again, but unity didn’t complain with any errors. I suppose I’ll never figure it why clip is acting like that, but now I know not to use clip as a variable. heh.

I still don’t understand the problem. I use clip as a variable name all the time. There is no reason not to…

Honestly, same here… If you don’t know, I for sure do not know. If you’d like, you can give it a go by downloading the github thingy. I just hope that it really is a one time thing.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms