Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LPGaming · Jan 11, 2013 at 09:30 AM · c#erroraistatshealth

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), "");
     }
     
 }
 
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cdrandin · Jan 11, 2013 at 09:47 AM 0
Share

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

avatar image LPGaming · Jan 11, 2013 at 03:57 PM 0
Share

Thanks, I'll check it out.

avatar image LPGaming · Jan 11, 2013 at 04:14 PM 0
Share

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.

avatar image JecoGames · Jan 12, 2013 at 12:30 AM 0
Share

Dont you need to say at the start of the levelup script using Health.//put whatever method the integers in in here ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LPGaming · Jan 12, 2013 at 03:45 AM 0
Share

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.

avatar image robertbu · Jan 12, 2013 at 07:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges