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 /
This question was closed Aug 22, 2014 at 06:50 AM by HexadimensionalerAlp for the following reason:

found own solution

avatar image
0
Question by HexadimensionalerAlp · Aug 06, 2014 at 11:00 PM · c#fpsaccessing from any script

Access variables from other Script via Properties [C#]

I try to make a FPS but I have the problem that Unity gets an StackOverflowExeption. I ask myself if it is because of the get{} set{} and if so what is my mistake? Or is there a better way if I want to have more than just one gun?

Here is my GunScript that is attached to the player and holds the shooting mechanic:

 using UnityEngine;
 using System.Collections;
 
 public class GunScript : MonoBehaviour {
 
     private float fireRate;
     public float FireRate
     {
         get{return FireRate;}
         set{FireRate = value;}
     }
     private float damage;
     public float Damage
     {
         get{return Damage;}
         set{Damage = value;}
     }
     private float reloadeTime;
     public float ReloadTime
     {
         get{return ReloadTime;}
         set{ReloadTime = value;}
     }
     private float nextFire;
     private int clipSize;
     public int ClipSize
     {
         get{return ClipSize;}
         set{ClipSize = value;}
     }
     public int ammo;
 
     private int ammoLeft;
 
     private int firemode;
     private int clipsLeft;
     public int ClipsLeft
     {
         get{return ClipsLeft;}
         set{ClipsLeft = value;}
     }
     private int roundsPerBurst;
     public int RoundsPerBurst
     {
         get{return RoundsPerBurst;}
         set{RoundsPerBurst = value;}
     }
 
     public bool wait;
     public bool isBursting;
 
     void Start () 
     {
         ammo = clipSize;
     }
 
     void Awake () 
     {
         fireRate = FireRate;
         damage = Damage;
         clipSize = ClipSize;
         reloadeTime = ReloadTime;
         roundsPerBurst = RoundsPerBurst;
         clipsLeft = ClipsLeft;
 
     }
     
     void Update () 
     {
         if (Input.GetKey (KeyCode.V)) 
         {
             firemode ++;
             if(firemode > 3)
             firemode = 0;
         }
 
         if (firemode == 0) 
         {
             if (Input.GetButton ("Fire1") && Time.time > nextFire && wait == false) 
             {
                 if (ammo > 0) 
                 {
                     nextFire = Time.time + fireRate;
                     RaycastShot ();
                     audio.Play ();
                     ammo--;
                 }
             }
         }
         
         if (firemode == 1) 
         {
             if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && wait == false) 
             {
                 if (ammo > 0) 
                 {
                     nextFire = Time.time + fireRate;
                     RaycastShot ();
                     audio.Play ();
                     ammo--;
                 }
             }
         }
 
         if (firemode == 2) 
         {
             if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && wait == false && isBursting == false) {
                 if (ammo > 0) 
                 {
                     nextFire = Time.time + fireRate;
                     StartCoroutine(Burst_Fire ());
                     audio.Play ();
                     ammo--;
                 }
             }
         }
 
         if(Input.GetKeyDown(KeyCode.R) && clipsLeft > 0)
         {
             StartCoroutine(Reload ());
         }
     }
 
     public IEnumerator Reload () 
     {
         if (clipsLeft > 0) 
         {
             if (ammo > 0) 
             {
                 clipsLeft--;
                 wait = true;
                 //audio.Play();
                 //animation.play();
                 yield return new WaitForSeconds (ReloadTime);
                 ammo = clipSize + 1;
                 wait = false;
             } 
             else 
             {
                 clipsLeft--;
                 wait = true;
                 //audio.Play();
                 //animation.Play();
                 yield return new WaitForSeconds (ReloadTime);
                 ammo = clipSize;
                 wait = false;
             }
         }
     }
 
         IEnumerator Burst_Fire()
         {
             int shotCounter = 0;
             // Keep firing until we used up the fire time
             while (nextFire < Time.time)
             {
                 while (shotCounter < roundsPerBurst)
                 {
                 isBursting = true;
                     RaycastShot ();
                     shotCounter++;
                     yield return new WaitForSeconds(0.1f);               
                 }           
                 
                 nextFire += fireRate;
             }
         isBursting = false;
         }
 
     private void RaycastShot ()
     {
         Ray ray = Camera.main.ViewportPointToRay(new Vector3(Screen.width*0.5f, Screen.height*0.5f, 0f));
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, 300)) 
         {
             if(hit.collider.tag == "EnemyHead")
             {
                 Vector3 hitPoint = hit.point;
                 float Damage = damage * 5;
                 hit.transform.SendMessage ("ApplyDammage", Damage, SendMessageOptions.DontRequireReceiver);
                 Debug.DrawLine (hit.point, hit.point + hit.normal, Color.cyan);
                 Debug.Log("Hit");
             }
             if(hit.collider.tag == "EnemyBody")
             {
                 Vector3 hitPoint = hit.point;
                 float Damage = damage ;
                 hit.transform.SendMessage ("ApplyDammage", Damage, SendMessageOptions.DontRequireReceiver);
                 Debug.DrawLine (hit.point, hit.point + hit.normal, Color.cyan);
                 Debug.Log("Hit");
             }
             else
                 Debug.Log("Miss");
         }
     }
 
 }


And here is the script that holds the values.It is attached to the particular gun:

 using UnityEngine;
 using System.Collections;
 
 public class AssaultRifle : MonoBehaviour {
 
     private float damage = 28f;
     private float fireRate = 0.1f;
     private int clipSize = 30;
     private int roundsPerBurst = 3;
     private int firemode;
 
     GunScript ActualGun = new GunScript ();
     
     void Awake () 
     {
         ActualGun.ClipSize = clipSize;
         ActualGun.Damage = damage;
         ActualGun.FireRate = fireRate;
         ActualGun.RoundsPerBurst = roundsPerBurst;
     }
 }
 

 

Thank you for your answer :D

Comment
Add comment · Show 2
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 HexadimensionalerAlp · Aug 07, 2014 at 07:53 PM 0
Share

$$anonymous$$y problem is that I found no good tutorial in c# where it is explained how to code in a way that you are able to go to a gun, pick it up and the script with the shooting mechanics knows witch gun is selected and gets the variables from the script attached to the gun. If you know how to do it please explain it to me, if you know a good tutorial please send it to my or if you know a better way please tell me because I haven't solved this problem for nearly a week now. Thanks in advantage

avatar image Kiwasi · Aug 07, 2014 at 07:57 PM 0
Share

Everything you are interested in doing can be found in the beginner tutorials

1 Reply

  • Sort: 
avatar image
1

Answer by AyAMrau · Aug 06, 2014 at 11:34 PM

What you are currently doing is having the properties access itself (which is bad), you should refer to the variables instead:

 private float fireRate;
 public float FireRate
 {
     get{return fireRate;}
     set{fireRate = value;}
 }


Also what are you trying to achieve in the Awake() function? When the properties are fixed all that would do is assign the variable values to themselves.

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 Kiwasi · Aug 06, 2014 at 11:43 PM 0
Share

In this case it might be better to ignore properties altogether:

 public float FireRate;

or use auto properties

 public float FireRate {get; set;}

Both methods will achieve the same result

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unable to access variable from other C# script? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script can't find Component within Start() - C# 1 Answer

C# Rocket Explode On Impact 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