Problem with setting an int from another script

Hello everyone,

I’m new to Unity and currently doing some exercises from a YouTube tutorial series by VegetarianZombie. Everthing has been going great so far, but now I have encountered an issue. I want to set a value to a private int from another script.

This works great, as long as the Set-Value funct. and the Return-Value funct. are being called from the same function. However, if I simply set the value of the int at void Start() and later on want to call it from OnDisable(), it seems to have disappeared, or not to have been saved at all.

Here, I want the cube to acces playerStruct.
What is weird that the value gets printed without an issue by the Debug.Log in Start(), but I just can’t figure out what is wrong with OnDisable().

Thank you so much for your help guys. Have a good one!

playerStruct

namespace Tutorial.Alien
{

    public struct Adversary
    {

        private int damage;
        private int Healthpool;
        private bool IsAlive;




        public void SetDamage(int damage)
        {
            this.damage = damage;
        }

        public int GetDamage()
        {
            return damage;
        }






    }

}

cube - script

using System.Collections;
using System.Collections.Generic;
using Tutorial.Alien;
using UnityEngine;

public class cube : MonoBehaviour
{
    Adversary Alien;

    void Start()
    {

        Adversary Alien = new Adversary();
        Alien.SetDamage(100);
        Debug.Log(Alien.GetDamage());
      
    }

    void OnDisable()
    {

     
        Debug.Log("The alien deals " + Alien.GetDamage () + " points of damage.");
    }
}

EDIT: healthpool and IsAlive don’t matter of course, I want to get damage right first

Post your code here with code tags. Not many will download a file to view it.

1 Like

Thank you Daniel I have edited my OP

Hey Daniel, the problem is actually pretty straightforward. You have a variable of type Adversary that is global to the class cube. The type Adversary is already declared, but then your declaring it again in the start method. So remove the type declaration, and just use the variable’s name when you assign it in the start method.

edit: spelling errors

1 Like

Since this language might be a little bit dense: remove that word “Adversary” from line 13.

2 Likes

Zullin and StarManta, thank you so much for your responses. It’s working! :slight_smile:

I guess this is a general rule I can learn from this. Is it always so dangerous to call methods twice? Or does this specifically have to do with classes?

Again, thank you so much for your help. It really is appreciated!

It’s not that you’re calling a method twice. When you say “Adversary Alien” in Start, you’re creating a new local Adversary. With the local one in existence, the compiler assumes that “Alien” refers to the local one… and none of your code affected the class member, which is what gets referenced elsewhere.

If you don’t declare it there, the compiler will instead find and use the class’s “Alien” member.

Personally, I wish it’d throw errors for local variables named the same as a class member. I’ve never once seen an instance where that was the intended behavior.

1 Like

You weren’t calling a method in this situation. You were declaring a variable with the same name in a more localized scope. You were therefore overriding the previous declaration (in that scope only*).
There’s nothing wrong/illegal about doing so, but as you saw – in your situation, it meant giving an unintended result. :slight_smile:

1 Like

Guys thank you so much for taking your time and helping me out, have a great day! :slight_smile: