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 /
avatar image
0
Question by jonathanbooker2911 · Dec 21, 2018 at 03:25 AM · scripting problemscript.scripting beginnerscript error

How to share varibles betweent scripts

 public class Weapon : MonoBehaviour
 {
      public float normalDamage;
      public float fireRate;
      public float damage = 5;
      public float range;
      public float radius;
      public float reloadTime;
      public float fireTimer;
      public float delay;
      public float basedamage;
 }

 public class Projectile : MonoBehaviour 
 {
       void OnCollisionEnter(Collider other)
        {
          if(other.GetComponent<EnemyHealth>() != null)
          {           
             other.GetComponent<EnemyHealth>().TakeDamage(damage);
           }
       }
 }

How do I get the damage from my weapon scripts to work in my projectile script. I've tried so many other methods and it just not working.

Comment
Add comment · Show 1
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 fafase · Dec 21, 2018 at 04:59 AM 0
Share

I edited your post, I assume it was wrongly pasted as the initial code would not compiled (you were missing a } to close the Weapon class). Please review the edit.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Dec 21, 2018 at 05:01 AM

you use a component lookup like this:

 GetComponent<nameOfScriptYouAreLookingFor> ().somevariable = true;
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
avatar image
0

Answer by cdr9042 · Dec 21, 2018 at 05:11 AM

There are many ways, here is one:

In the weapon's shoot function:

 GameObject proj = Instantiate( //your instantiate code );
 proj.GetComponent<Projectile>().Init(this); //pass reference to this instance

Put this in Projectile:

 public Weapon m_Weapon;
 
 public void Init(Weapon weapon)
 {
      m_Weapon = weapon;
 }

then in your deal damage code

 other.GetComponent<EnemyHealth>().TakeDamage(m_Weapon.damage);

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

Answer by fafase · Dec 22, 2018 at 09:53 AM

Here needs be considered the architecture and logic. Most likely a Weapon should include a projectile as it is the logical way of things (You use a certain type of bullet with a given rifle in real life, not the other way around).

So your weapon class would include the Projectile it is using. This way you can pass how much damage a projectile should do based on what weapon is using it, just like in real.

Your weapon class should handle the input so when user presses a button, a projectile is created (or used from a pool) and the damage value is given to it.

 public class Weapon : MonoBehaviour
 {
       [SerializedField] protected GameObject projectile;
       [SerializedField]protected float damage;
       protected virtual void Update(){
            if(Input.GetKeyDown(KeyCode.Space)){  CreateProjectile(); }
      }
      protected virtual void CreateProjectile(){
            GameObject obj = (GameObject) Instantiate(this.projectile);
            obj.GetComponent<Projectile>().Init(this.damage);
     }
 } 

 pubic class Projectile : MonoBehaviour
 {
       [SerializedField] protected float initialDamage;
       protected float actualDamage;
       public virtual void Init(float weaponDamage){  
              // Here we could say that projectile has initial damage that is multiplied by weapon damage
              this.actualDamage = this.initialDamage * weaponDamage;
       }
       protected virtual void OnCollisionEnter(Collider other)
        {
           EnemyHealth ea = other.GetComponent<EnemyHealth>();
           if(ea == null){ return; }
           ea.TakeDamage(this.actualDamage);
        }
 }

Notice that most methods and references are protected and/or virtual. This is because you should make your Weapon and Projectile classes as abstract. You don't find a weapon in real, you find a AK-47 that is a weapon. So you could consider a subclass Ak that inherits from Weapon and takes care of its own data.

As for the code, when the space bar is pressed down, it will create a new projectile and on creation, it will pass down some data (there could be more than just a float). This is simple but could get you going in a good direction.

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

Answer by H1ddeNGames · Dec 22, 2018 at 11:09 AM

I am assuming your Weapon class can make things like Axes, Swords, and projectile throwing weapons like Arrow/Bow and Throwing Knives. If this is a correct assumption then you can do something like this:


   public class Projectile : MonoBehaviour 
      {
     // Get a reference to the Weapon that created this Projectile. You would set this in the inspector. 
     public Weapon weapon; 
     
     // Since the variables in your Weapon class are public you can call on them like this:
     Debug.Log(weapon.damage); 
     
            void OnCollisionEnter(Collider other)
             {
               if(other.GetComponent<EnemyHealth>() != null)
               {           
                  other.GetComponent<EnemyHealth>().TakeDamage(weapon.damage);
                }
            }
      }
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

181 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 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 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 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 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 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

Need help fixing my script 1 Answer

How do I make my projectile deal damage to a target it hit. 1 Answer

How do I make do I make a variable go back to its original value for a spell system 1 Answer

3rd Person Camera to Rotate With the Player 0 Answers

Slowly increase motor.force 1 Answer


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