2

So some important things to let you guys know is this is in unity and this script is on a gameObject(the key). It casts a ray and then with hitinfo.transform.SendMessage("interactedWithItem"); this function below is called from another script. I can state that the Debug.Log("this is a key") is triggerd and shows in console, but for what ever reason the value of keyAmount isn't incremented. What am I doing wrong?

public int keyAmount;
public bool storableItem = true;
bool key = true;

//The function that will run if the storable item is set to true
public void interactedWithItem()
{

    //Checks if the object is a key
    if (key)
    {
        keyAmount++;
        Debug.Log("is a key");
    }

    gameObject.SetActive(false);
}
kara
  • 3,205
  • 4
  • 20
  • 34
  • 2
    How are you checking whether the value changed? – UnholySheep Apr 26 '18 at 11:19
  • See https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i , it may solve your Problem (i++ vs ++i) – Chrᴉz remembers Monica Apr 26 '18 at 11:20
  • 1
    Did you check without the `SetActive(false)`-line? Changing a value in a script you disable afterwards is probably not a good idea. – Gunnar B. Apr 26 '18 at 11:20
  • Sorry guys it appears that it was a silly mistake on my behalf. Changing the keyAmount to be static worked and cleared up all the problems thanks for all the help everyone. Still doesn't explain why the value didn't change in the unity inspector, in that particular instance of that prefab. Strange but thanks for the help all. – James Logan Apr 26 '18 at 11:44
  • Almost all values that are modified during run-time ('play') are reset when you go back to design-time ('stopped'). As you noted in your comments to one of the answers below, during run-time your increment is working as intended. You may want to look into the *Object Lifecycle* a bit more. – Immersive Apr 28 '18 at 07:57
  • As to why it doesn't update in the inspector during run-time, it sounds like you didn't have the active instance of the object selected. – Immersive Apr 28 '18 at 07:58

1 Answers1

2

I think your script is instanciated when it is called. Because of this your variables are reset. Try this example:

public class MyClass
{
    static void Main(string[] args)
    {
        MyClass myObject = new MyClass();
        myObject.Increase();
        myObject.Print(); // output: 1, 1

        myObject = new MyClass(); // new instance => only static variables are stored in the class and will not be dismissed.
        myObject.Increase();
        myObject.Print(); // output 1, 2
    }

    private int NotStaticItem = 0; // one per instance/object
    private static int StaticItem = 0; // one per class

    public void Increase()
    {
        NotStaticItem++;
        StaticItem++;
    }

    public void Print()
    {
        Console.WriteLine("NotStaticItem: {0}", NotStaticItem);
        Console.WriteLine("StaticItem: {0}", StaticItem);
    }
}

Build two KeyCounts. One static and one not-static. If the static value is not being reset you know, that unity builds new objects in this case.

kara
  • 3,205
  • 4
  • 20
  • 34
  • oh my i have realised what i have done. The key is a prefab so i'm guessing thats why the value won't change. Though i added a Debug.Log(keyAmount) and it does increment by 1. The prefab is only a used as a reference in a gameObject variable. Maybe i should make it a static variable for one. So where exactly can i get access to that particular instance of the prefab. Btw i'm checking the value through the editor. Sorry if that doesn't make much sense. – James Logan Apr 26 '18 at 11:34
  • 1
    @JamesLogan Prefabs have nothing to do with your problem, other than by instantiating a new copy of the prefab, you also create *new copies of your script* with the same values that they had when you created the prefab. – Draco18s no longer trusts SE Apr 26 '18 at 17:09
  • @Draco18s actually, it makes sense if he's been watching the prefab in the inspector but expecting to see the instance values... ;) – Immersive Apr 28 '18 at 08:00