• 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 awplays49 · Nov 22, 2014 at 02:32 AM · notworking

How do I CHANGE an int from one script by doing GetComponent from another?

I wrote this script, but the PlayerHealthGet just changes itself, not the int I've ordered it to "find".

 using UnityEngine;
 using System.Collections;
 
 public class Attacking : MonoBehaviour {
 
     // Gives the monster something to attack
     private GameObject Target;
 
     // Basic properties
     public int Damage;
     public int Health;
     public float Speed;
 
     // Basic distance floats
     public float GeneralDistance;
     public float AttackDistance;
     public float GiveUpDistance;
     public float AttackRange;
 
     // Advanced distance floats
     private float Distance;
 
     // Grab player properties
     private GameObject PlayerGet;
     private int PlayerHealthGet;
 
     // Grab human follower properties
     private GameObject HumanFollowerGet;
     private int HumanFollowerHealthGet;
     private bool HumanFollowerStateGet;
 
     // Grab alien follower properties
     private GameObject AlienFollowerGet;
     private int AlienFollowerHealthGet;
     private bool AlienFollowerStateGet;
 
     // Decides whether or not the monster can attack
     private bool CanAttack;
 
     // Sets up the state machine where the monster decides what it wants to do
     private enum AttackOrGiveUp {Attack, GiveUp};
     private AttackOrGiveUp AttackState;
 
     void Start () {
         // Gives the monsters a target to start with
         Target = PlayerGet;
 
         // Gives "Grab player properties" code
         PlayerGet = GameObject.Find ("Player");
         PlayerHealthGet = PlayerGet.GetComponent <Player> ().Health;
 
         // Gives "Grab human follower properties" code
         HumanFollowerGet = GameObject.Find ("Human Follower");
         HumanFollowerHealthGet = HumanFollowerGet.GetComponent <HumanFollower> ().Health;
         HumanFollowerStateGet = HumanFollowerGet.GetComponent <HumanFollower> ().Following;
 
         // Allows the monster to deal damage as soon as the game is started
         CanAttack = true;
     }
 
     void Update () {
         // If the human follower is following the player
         if (HumanFollowerStateGet == true)
         {
             // Make the follower to attack the human follower
             Target = HumanFollowerGet;
         }
         // If the alien follower is following the player
         if (AlienFollowerStateGet == true)
         {
             // Make the follower to attack the alien follower
             Target = AlienFollowerGet;
         }
         // If the player has no followers
         if (Target == null)
         {
             // The monster's target will once again become the player
             Target = PlayerGet;
         }
 
         // Gives "Advanced distance floats" code
         Distance = Vector2.Distance (PlayerGet.rigidbody2D.transform.position, rigidbody2D.transform.position);
 
         // If the monster sees the player in range
         if (Distance <= AttackDistance)
         {
             // The monster will attack the player
             AttackState = AttackOrGiveUp.Attack;
         }
 
         // If the monster sees neither the player or follower
         if (Distance >= GiveUpDistance)
         {
             // The monster will give up on attacking the player and the follower
             AttackState = AttackOrGiveUp.GiveUp;
         }
         // If the monster is following the follower
         if (AttackState == AttackOrGiveUp.Attack)
         {
             // If the follower's x value is greater than the sum of the monster's x value and the general distance set
             if (Target.rigidbody2D.transform.position.x > rigidbody2D.transform.position.x + GeneralDistance)
             {
                 // Move the monster to the right to catch up with the follower
                 rigidbody2D.transform.position += Vector3.right * Speed * Time.deltaTime;
             }
             // If the follower's x value is less than the difference between the monster's x value and the general distance set
             if (Target.rigidbody2D.transform.position.x < rigidbody2D.transform.position.x - GeneralDistance)
             {
                 // Move the monster to the left to catch up with the follower
                 rigidbody2D.transform.position += Vector3.left * Speed * Time.deltaTime;
             }
             // If the follower's y value is greater than the sum of the monster's y value and the general distance set
             if (Target.rigidbody2D.transform.position.y > rigidbody2D.transform.position.y + GeneralDistance)
             {
                 // Move the monster up to catch up with the follower
                 rigidbody2D.transform.position += Vector3.up * Speed * Time.deltaTime;
             }
             // If the follower's y value is less than the difference between the monstqer's y value and the general distance set
             if (Target.rigidbody2D.transform.position.y  < rigidbody2D.transform.position.y - GeneralDistance)
             {
                 // Move the monster down to catch up with the follower
                 rigidbody2D.transform.position += Vector3.down * Speed * Time.deltaTime;
             }
 
             // If the target is in the range of an attack and the monster hasn't attacked in the past second
             if (Distance <= AttackRange && CanAttack == true)
             {    
                 // Do everything mentioned in the Attack function
                 Attack ();
             }
         }
     }
 
 
     void Die () {
     }
 
     void Attack () {
         // Change the current health of the target to the difference between the current health and the damage set
         PlayerHealthGet -= Damage;
 
         // Start the coroutine that delays the attack
         StartCoroutine (AttackDelay ());
     }
 
     IEnumerator AttackDelay () {
         // The monster cannot attack
         CanAttack = false;
 
         // Wait one second
         yield return new WaitForSeconds (1);
 
         // The monster can attack again
         CanAttack = true;
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by Jeff-Kesselman · Nov 22, 2014 at 02:35 AM

You clearly dont understand the unity component/object model, let me try to explain it to you...

THIS line, loads the health value from the gameObject called "Player" into a local field in THIS game object called PlayerHealthGet

 PlayerGet = GameObject.Find ("Player");
 PlayerHealthGet = PlayerGet.GetComponent <Player> ().Health;

THIS modifies the **local* field, NOT the value on the game object called Player

   PlayerHealthGet -= Damage;

You never set the field on the Player game object. So, it never changes.

Comment
Add comment · Show 1 · 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 awplays49 · Nov 22, 2014 at 03:08 AM 0
Share

already figured it out, thanks though

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Menu not working properly 1 Answer

OnTriggerStay2D Broken 2 Answers

Building the scenes 1 Answer

Unity 5.1.3f1 won't open 0 Answers

why wont machine gun follow first person controller 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges