Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Maemmo · Nov 06, 2015 at 04:21 PM · c#methodcalling

How do I call a method from another script?

I have been searching the web for a long time, but I can't find an answer that I can use. (This is my first Unity game ever. I'm following a course on youtube and copy what he does. Just for learning how to do things. So I tried to make my own ammo system, where I almost did the same as he did with health(I have removed health, so it's easier to read.), and I finally got it to work (along with some errors I had to fix by making this script a little strange, which you might notice).). The method in the player script is called "UseAmmo", and is supposed to decrease my ammo by. The problem is that I don't know how to call it from the weapon script when I shoot. Please help me :D Here's my player script:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

 [System.Serializable]
 public class PlayerStats
 {
     [SerializeField]
     private StatusIndicator statusIndicator;

     public static int WeaponDamage = Random.Range(1, 1);

     public int maxAmmo = 200;
     public int startAmmo = 50;

     private int _curAmmo;
     public int curAmmo
     {
         get { return _curAmmo; }
         set { Mathf.Clamp(value, 0, maxAmmo); }
     }
     

     public void Init()
     {
         _curAmmo = startAmmo;
     }
     public void UseAmmo(int minAmmo)
     {
         _curAmmo -= minAmmo;
         if (_curAmmo == 0)
         {
             nullifyDamage();
         }
         // statusIndicator.SetAmmo(curAmmo, maxAmmo);
     }
     public void GetAmmo(int plusAmmo)
     {
         _curAmmo += plusAmmo;
         if (_curAmmo > 0)
         {
             createDamage();
         }
         statusIndicator.SetAmmo(curAmmo, maxAmmo);
     }
     public void nullifyDamage()
     {
         WeaponDamage = 0;
     }
     public void createDamage()
     {
         WeaponDamage = Random.Range(20, 50);
     }
 }

public PlayerStats stats = new PlayerStats();

 [SerializeField]
 private StatusIndicator statusIndicator;

 void Start()
 {
     
     stats.Init();

     if (statusIndicator == null)
     {
         Debug.LogError("No status indicator referenced on Player");
     }
     if (statusIndicator != null)
     {
         statusIndicator.SetAmmo(stats.curAmmo, stats.maxAmmo);
     }
 }

//More script down there, which doesn't matter

And now a little bit of my weapon script (which is working fine besides the problem I mentioned). I have also removed the parts of the script that didn't matter:

using UnityEngine; using System.Collections;

public class Weapon : MonoBehaviour { void Shoot() { // I would like to call the method here. } // More script

}

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

Answer by Maemmo · Nov 06, 2015 at 04:22 PM

Sorry, it turned out to look quite strange in the last script, but I hope you get what means.

Comment
Add comment · 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

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] Why is this method called twice? 0 Answers

Use a class like a function 1 Answer

Make a virtual method englobing a child override function 0 Answers

How to call a method from a class in a different script in c# 1 Answer

Calling a function from another script 0 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