0

Currently, I'm trying to add / subtract value from one script to another. I wanted script one to add +125 health to script two but don't know how. There is no gameobject involved in this scenarios.

Script one is

using UnityEngine;
using System.Collections;

public class AddHealth : MonoBehaviour {

    int health = 10;

    public void ChokeAdd()

    {
        AddHealthNow();
    }

    public void AddHealthNow()
    {
        health += 125;
        Debug.Log("Added +125 Health");
    }
}

Script two is

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

namespace CompleteProject
{
    public class DataManager : MonoBehaviour

    {
        public static int depth;        
        public Text BOPPressureText;
        int health = 20;

        void Awake ()

        {
            depth = 0 ;
        }

        void Update ()
        {
            BOPPressureText.text = depth + 7 * (health) + " psi ";
        }
    }
}
Coder
  • 499
  • 3
  • 13
  • 29
  • 1
    "There is no gameobject involved in this scenarios." That's a pretty big claim when you're working in Unity, and very important. How did you make these components without associating them with a gameobject? – 31eee384 Jan 08 '16 at 19:26

1 Answers1

1

If you're trying to add health to your second script, declare your health field as public. So that you can access its value in your first script.

public int health;

But I wouldn't do stuff like that. Expose this field by a property like:

public int Health 
{
 get 
   {
    return this.health;
   }
 set 
   {
    this.health = value;
   }
}

by default the health will be declared as

private int health;

Other scripts can't access private fields. Also you need a reference to your second script. You can access this via:

public DataManager data;

You've to assign your second object into this field in your Unity Editor. Then This way, you can access the field healthby calling data.health += 125 in your first script.

I don't know the specific thing in Unity, but I think you can also call your script by:

DataManager data = GetComponent<DataManager>();
data.health += 125;

Other method to get your other script is call it like that in your first script:

var secondScript = GameObject.FindObjectOfType(typeof(DataManager)) as DataManager;
secondScript.health += 125;
Christoph K
  • 344
  • 6
  • 16
  • I having some errors adding DataManager data = GetComponent(); on the second script. – Coder Jan 08 '16 at 18:56
  • Do you have a reference to your first script, is it in the same namespace, if not considering adding it to your usings. – Christoph K Jan 08 '16 at 18:58
  • No, it doesn't have any reference. – Coder Jan 08 '16 at 19:00
  • I'm currently getting this error "The type or namespace name `DataManager' could not be found. Are you missing a using directive or an assembly reference?" – Coder Jan 08 '16 at 19:03
  • If you're using Visual studio try right clicking and "Resolving". Otherwise add a reference to your second script inside your first. – Christoph K Jan 08 '16 at 19:57
  • Ok. I will give it a try. Thanks. – Coder Jan 08 '16 at 20:04
  • In Unity3d, you don't ever call a constructor of a class which inherited from MonoBehavior. That is improper. Do not declare public variables either except for access through the Unity Editor. – xoxox Jan 08 '16 at 20:07
  • Look at the edited post. You've several options to get your script. But expose your health field. Then use one of these methods, which I edited in the post. – Christoph K Jan 08 '16 at 20:21
  • Final got it to work! Thanks! I have to removed the namespace "Complete Project" first and use your 2nd method you have proposed. – Coder Jan 09 '16 at 00:54