• 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 Joxerbrown · Aug 11, 2015 at 12:06 PM · scoreint

How do I add 1 int to another script

Hi all, so I have a script as below, which gets no errors but does not do what I want it to do, I am trying to add 1 int to the score var in the Shoot script from this script, but it adds multiples to the score var in the Shoot script, would anyone know how I can fix this. Thank you.

  using UnityEngine;
  using System.Collections;
  
  public class TargetHealthA : MonoBehaviour 
  {
      public float health = 35f;
      public Animation targetDown;
      public float lessOne = 0.1f;
      public int addOne = 1;
      public Shoot addScore;
  
      
      void Start()
      {
          targetDown = GetComponent<Animation> ();
          addScore = GetComponent<Shoot> ();
      }
      
      public void RemoveHealth(float enemyDamage)
      {
          health -= enemyDamage;
      }
      
      void Update()
      {
          if (health <= 0) 
          {
              Dead ();
          }
      }
  
      void Dead()
      {
          Shoot.score += 1;
          targetDown.Play ("TargetDownA");
          Destroy (gameObject, 0.5f);
      }
  }



I have already tried to use using

 using UnityEngine; 
 using System.Collections; 
 
 public class TargetHealthA : MonoBehaviour 
 { 
    public float health = 35f;
    public Animation targetDown;
    public float lessOne = 0.1f;
    public Shoot addScore; 
 
    void Start() 
    { 
       targetDown = GetComponent<Animation> ();
       addScore = GetComponent<Shoot> ();
    } 
 
    public void RemoveHealth(float enemyDamage) 
    { 
       health -= enemyDamage; 
    } 
 
    void Update() 
    { 
      if (health <= 0)
      {
        Dead ();
      }
    }
 
    void Dead() 
    { 
       GetComponent<Shoot>(), AddScore();
       targetDown.Play ("TargetDownA"); 
       Destroy (gameObject, 0.5f);
    } 
 }

For the line GetComponent(),AddScore(); I do not know what is meant to go infront of this, I have searched through Answers but most responses are about how to set it up as in void Start varName = GetComponent();, but dont explain how to access the script through another function.

The Shoot Script is about 600 lines long so I cant post it here, but everything works perfectly in it bar adding the 1 int when the Target is hit, but here is the section for the score

    void Update() 
    { 
       scoreText.text ="Score : "+ score.ToString 
    }
 
    public void AddScore(int addScore) 
    { 
       score += addOne; 
    }'

As I say when I use Shoot.score += 1 from the first script shown I get multiples adding to the score not just 1 int as I am looking for. Could someone please explain to me how to fix these issues. Thank you. Joxer

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
1

Answer by YoungDeveloper · Aug 11, 2015 at 12:07 PM

I explain it very clearly here:

http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html

and here

http://answers.unity3d.com/questions/597617/how-do-i-addsubtract-variables-between-two-differe.html

Comment
Add comment · Show 4 · 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 Joxerbrown · Aug 11, 2015 at 12:27 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object TargetHealthA.Dead () (at Assets/! Scripts/TargetHealthA.cs:35) TargetHealthA.Update () (at Assets/! Scripts/TargetHealthA.cs:29)

Hi YoungDeveloper, I get this error when I try to do the first link you posted.

 using UnityEngine;
 using System.Collections;
 
 public class TargetHealthA : $$anonymous$$onoBehaviour 
 {
     public float health = 35f;
     public Animation targetDown;
     public float lessOne = 0.1f;
     public int addOne = 1;
     public GameObject otherObject;
     Shoot addScore;
 
     
     void Start()
     {
         targetDown = GetComponent<Animation> ();
         addScore = otherObject.GetComponent<Shoot> ();
     }
     
     public void RemoveHealth(float enemyDamage)
     {
         health -= enemyDamage;
     }
     
     void Update()
     {
         if (health <= 0) 
         {
             Dead ();
         }
     }
 
     void Dead()
     {
         addScore.AddScore (addOne);
         targetDown.Play ("TargetDownA");
         Destroy (gameObject, 0.5f);
     }
 }
avatar image Joxerbrown · Aug 11, 2015 at 12:48 PM 0
Share

Ok,the Shoot script is attached to an Empty GameObject which is a child of another gameobject, do I need to use GetComponentInChildren, I didn't think I would need to as the Shoot script is on the child.

avatar image YoungDeveloper · Aug 11, 2015 at 01:31 PM 1
Share

If you are having problems - Use public variables, simply drag and drop you script to public variable. Exactly like in first link example. Also

if (health

should be done in removehealth() and not update.

avatar image Joxerbrown · Aug 11, 2015 at 02:46 PM 0
Share

Cheers, that has brought down the random increments to now being 3 to 5, its still not doing the 1 at a time as I had hoped for but its a little closer, thanks.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i link a GUI text to a variable text which is a integer? 2 Answers

Multiply float by int? 2 Answers

Check if a guiText = a number? 2 Answers

int +=1 won't exceed 1 2 Answers

Adding coin score to multiplied value 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