- Home /
found own solution
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
$$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
Everything you are interested in doing can be found in the beginner tutorials
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.
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
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