Error in code (I'm new)

Hi! Cheers for clicking on me :slight_smile:

So I’m really new to C# so this might be a really easy fix but I’m completely stuck, this is the first code I’ve written.

Explanation of the code

Okay so basically this is just a normal clicker game.

The weapon is a variable that ranges from 1 and continues up.
So if Weapon=1 then Weapon then the text should equal Stick and so on. (this is how I use to do it in Python xD)

I know I repeated public int Weapon = 1; (it doesn’t seem to make a difference, it was done to fix an error about script referencing and seemed to fix it)

Okay, any solutions are okay, I just really need some help :slight_smile:

This is WeaponBuy (the button that clicked upgrades the weapon)

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

public class WeaponBuy : MonoBehaviour
{
    public int Weapon = 0;
    public GameObject textBox;
    public GameObject statusBox;

    public void ClickaButton()
    {
        if (Weapon == 0)
        {
            if (GlobalCash.MoneyAmount < 40)
            {
                Weapon = 1;
            }
            else
            {
                statusBox.GetComponent<Text>().text = "You don't have enough cash!";
                statusBox.GetComponent<Animation>().Play("StatusAnim");
            }
        } 
    }
}

This is WeaponType (checks what weapon is so it knows what their weapon is eg. stick gun ect. Only stick is in there for now)

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

public class WeaponType : MonoBehaviour
{
    public int Weapon = 0;
    public GameObject WeaponDisplay;

    void Update()
    {
        if (Weapon == 0)
        {
            WeaponDisplay.GetComponent<Text>().text = "Stick";
        }
    }
}

Thank you :slight_smile:

What problem are you having?

1 Like

If you “fix” an error by doing something that doesn’t seem to make sense, there’s about a 99% chance that you’ve merely replaced an easy-to-spot error with an equally-wrong-but-harder-to-spot error.

Computer programming is like bargaining with evil genies: computers will grant your wishes, but you better phrase your wishes perfectly, because they’re going to give you exactly what you asked for, not what you meant. The computer will always follow your instructions down to the letter, no matter how stupid that is.

In the code you’ve posted, WeaponBuy and WeaponType now each have a separate variable called “Weapon”. Changing one of these variables has absolutely no effect on the other.

Also, if you have more than one WeaponBuy component attached to different objects in your scene, or if you have more than one WeaponType component attached to different objects in your scene, each copy of the class has a separate “Weapon” variable that is totally independent of all other copies.

If you want to access a variable that is inside of another component, you need a variable that contains a reference to the component you want to access.