- Home /
Need help calling a variable from another C# script
Here are my scripts, I'm trying to write a skillpoint selection for me and my friends game, he doesn't understand scripting at all that's why it's retardedly commented. Anyway, I'm trying to get it to set the health to the players Stamina times 10, but it returns 0.
Here are my two scripts
Health.cs (Simple AI downloaded, edited the code)
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float MaxHealth;
public float CurrentHealth;
public bool Invincible;
public bool Dead;
LevelUp levels = new LevelUp();
// Use this for initialization
void Start () {
//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
MaxHealth = levels.stamina * 10;
CurrentHealth=MaxHealth;
}
void LevelUp() {
MaxHealth = levels.stamina * 10;
CurrentHealth = MaxHealth;
}
// Update is called once per frame
void Update () {
//IF INVINCIBLE, HE CANNOT DIE..
if(Invincible){
CurrentHealth=MaxHealth;
}
else{
if(CurrentHealth<=0){
CurrentHealth=0;
Dead=true;
}
//MAX HEALTH
if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
//WHEN DEATH IS UPON HIM
if(Dead){
//TELL THE AI SCRIPT HE IS DEAD
FreeAI AI=(FreeAI)GetComponent("FreeAI");
if(AI){
if(AI.IsDead){}
else AI.IsDead=true;
}
}
}
}
}
and here is the LevelUp.cs for stat handling and experience.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelUp : MonoBehaviour {
/**
*
* int = Integer, A variable for a number without a decimal point.
* bool = Boolean, A variable that returns true or false.
* float = Integer, A variable for a number WITH Decimal Points.
* char = Character, A variable for a single character, I.E 'A' 'B' 'C'
* string = Group of characters, for sentences such at "Hello, JewFace"
*
**/
//I don't understand this either;'
public static LevelUp instance;
//Current Experience.
public int currExperience,
expToLevel, //Experience needed to level.
playerLevel; // Players Level
// Stat Points
public int strength, //Melee Damage
stamina, // Health Stat
agility, // Movement Agility, Archer Damage
dexterity, // CritRating - Acurracy.
intelligence; // Mana Scaling.
//This is called before script initialization.
void Awake() {
}
// Use this for initialization
void Start () {
instance = this;
strength = 10;
stamina = 10;
agility = 10;
dexterity = 10;
intelligence = 10;
currExperience = 0;
expToLevel = 100;
playerLevel = 1;
}
// Update is called once per frame
void Update () {
// Leveling up, if current experience is greater or equal to the required value, add experience to required level up value.
if(currExperience >= expToLevel) {
playerLevel += 1;
expToLevel = expToLevel + (int)(expToLevel * 1.1);
}
}
// Handles all GUI based functions, what's beign displayed.
void OnGUI() {
GUI.Box (new Rect(Screen.width / 2, Screen.height - 120, 260, 260), "");
}
}
See if this tutorial helps. I am currently following it and I have found it very helpful. Here is the part focused in Player Attributes. http://www.youtube.com/watch?annotation_id=annotation_953792&feature=iv&src_vid=1o7is2R1nUw&v=p8fiHeh$$anonymous$$ubs
Thanks, It looks like he's using JS while I use C#, usually converting isn't a problem but the C# language doesn't have the new Stat() constructor, which makes it very difficult to follow this tutorial.
Dont you need to say at the start of the levelup script using Health.//put whatever method the integers in in here
?
Answer by robertbu · Jan 11, 2013 at 05:02 PM
One thing jumps out at me in a quick read:
LevelUp levels = new LevelUp();
You are using the new operator on a class derived from Monobehavior. Monobehavior derived classes need to be attached to game objects. You can remove this line, attach the script to a game object, and then link up the two. Or don't derive the LevelUp class from Monobehavior. This means that you will need to change Start() to a class constructor and remove other Unity code like OnGUI(). Also the Update() method would need to be called from elsewhere (probably best to rename it as well).
Well, I did this in order to call the skill from Health.cs, I couldn't find any other way to do with besides for instancing, although the script is attatched to my playerController.
I'm not sure what you mean by instancing. But if have a Levels script attached to a game object. In you can create a public Levels variable in the health class and then just drag the object that contains the Levels script onto the public variable in the inspector.
If the Levels script is attached to the same game object as the Health class, you can also do something like: Levels levels = GetComponent(); to initialize the levels variable in your script.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Heath regain via Pickup code halp me 1 Answer
stupid errors i can't figure out 1 Answer
Argument out of range. 1 Answer
In game log not working. 2 Answers