I need some help...

Hi, cheers for clicking on this thread! :slight_smile:

Okay so basically I’m new to C# and as a start, I’m just building a simple clicker game. I’ve run into an error, that I have no clue to fix.

(Quick explanation on the code)
This is just checking a variable thats a number then assigning a weapon to that number (Eg. 1=Stick 10=Gun etc.)

Below is the error message, and screenshots of the two scripts. Anything would help at this point D;

Cheers :slight_smile:

Read the error and it explains what is happening.

“GameObject does not contain a definition for ‘Weapon’”. This means you’re trying to get “Weapon” from a GameObject, but “Weapon” only exists inside your “NewBehaviourScript” class which you should really rename (remembering that file name and class name must match).

In your “WeaponType” class, both your variables are of type GameObject. In order to access “Weapon”, you must have a reference to the “NewBehaviourScript” component. So you can either use GetComponent, similarly to what you have done to get the Text component, or you can change your variable type and assign the component in the inspector.

Hi and welcome!
So first of all, please use code tags to post code. Then, in case you post only a part of the code, (so not necessary here), saying which line the error references to helps as well. That said, if those were longer scripts, finding the correct line without line numbers next to the code would be impossible as well. Screenshots in general should be prevented (unless it’s actually about the image), so the error messages can just be copied most of the time. Also, a title should reflect your actual problem. If every thread was called “i need help”, how would you find what you are looking for? :slight_smile:

Ok with the above out of the way, your problem is as follows:
“WeaponBuy” is of type GameObject. A GameObject has no knowledge of what a “Weapon” is. What you proabably wanted to do instead, is reference an instance of your other script, meaning the type should be NewBehaviorScript instead of GameObject.
On that note, you probably want to rename that script (and the file) to something less generic.
And while i’m at it, common naming conventions for C# would like you to write your names in “camelCase” by default, and use UpperCamelCase for class, method or property names. That way they are easier to distinquish. While you are doing this for some variables like “textBox”, others you start with a capital letter, like for example “Weapon”.
Also, variables contain data, while methods contain instructions. As such it is common to name variables after what they contain (“name”, “weaponIndex”, …), while naming methods after what they do (“Attack()”, “Jump()”, “BuyWeapon()”, …). Some of your names make it really hard to guess what they represent. “ClickaButton”, for example, implies to me that it’s an object reference.

Hope this helps :slight_smile:

Anytime you do comparison, you need to use a double equals

if(WeaponBuy.Weapon == 0)

However, that being said, you should consider another way of updating your text only as needed instead of in Update.

Oh, and what the other posters said as I didn’t noticed your error post as they were small.

1 Like

Usually you should post code using code tags rather than screenshots, and include your error messages as text. (Although in this particular case the screenshot coincidentally revealed additional problems…)

That particular error occurs because your “WeaponBuy” variable isn’t specific enough. The particular object you dragged into that field in the inspector may have been something with a variable called “Weapon”, but you didn’t promise to the computer that it would be, you only promised it would be a GameObject. Most GameObjects don’t have a variable called “Weapon”.

You need to change the type of the variable from “GameObject” to something more specific.

But in order to tell you what to put there instead, we first need to discuss some other problems…

You have a file called WeaponBuy.cs but the class you defined inside of it is called NewBehaviourScript. That’s going to cause problems, because if you want to attach your script to an object in Unity, the name of the file and the name of the class have to match. I’m assuming you meant for the class to be called WeaponBuy.

But that will cause further problems, because “WeaponBuy” is already the name of a variable inside of your WeaponType script. You should make the class and the variable have different names so that you can tell them apart. (I don’t recall offhand whether the compiler will force them to have different names in this particular case, but even if it lets you make them the same, it’s a really bad idea.)

So for example, you might change the name of the class inside of WeaponBuy.cs to be “WeaponBuy”, and then you might change the relevant variable inside WeaponType.cs to be declared as something like “public WeaponBuy buy”. In this case, “WeaponBuy” says what kind of variable it is, and “buy” is the name of that specific variable.

1 Like