Accessing a non-static string variable in another script C#
I have a script with all of my enemies stats in a script called EnemyStats, but I need to access 2 string variables (AudioIntro and AudioMain) from another script which plays battle music which does not derive from EnemyStats. I know this has been answered many times before, but none of the answers helped me.
Here is the EnemyStats script
By the way EncryptFloat is a type of float I programmed to be encrypted, but I dont think that will be important for this.
using UnityEngine;
using System.Collections;
public class EnemyStats:MonoBehaviour {
public string EnemyName {
get{ return EnemyName; }
set{ EnemyName = value; }
}
public string EnemyDescription {
get{ return EnemyDescription; }
set{ EnemyDescription = value; }
}
public string EnemyEncounterText {
get{ return EnemyEncounterText; }
set{ EnemyEncounterText = value; }
}
public EncryptFloat Level {
get{ return Level; }
set{ Level = value; }
}
public EncryptFloat HP {
get{ return HP; }
set{ HP = value; }
}
public EncryptFloat Attack {
get{ return Attack; }
set{ Attack = value; }
}
public EncryptFloat Defense {
get{ return Defense; }
set{ Defense = value; }
}
public EncryptFloat Speed {
get{ return Speed; }
set{ Speed = value; }
}
public EncryptFloat Reward_EXP_Min {
get{ return Reward_EXP_Min; }
set{ Reward_EXP_Min = value; }
}
public EncryptFloat Reward_EXP_Max {
get{ return Reward_EXP_Max; }
set{ Reward_EXP_Max = value; }
}
public EncryptFloat AI {
set{ AI = value; }
}
public string AudioIntro {////////Variable I need
get{ return AudioIntro; }
set{ AudioIntro = value; }
}
public string AudioMain {////////Variable I need
get{ return AudioMain; }
set{ AudioMain = value; }
}
}
And here is the Battle script.
using UnityEngine;
using System.Collections;
public class Battle:MonoBehaviour{
public string AudioIntro;
public string AudioMain;
AudioSource AudioPlayer;
AudioClip Intro;
AudioClip Loop;
void GetAudio(){
AudioIntro = EnemyStats.AudioIntro; /////Returns error: An object reference is required for the non-static field, method or property 'EnemyStats.AudioIntro.get'
AudioMain = EnemyStats.AudioMain; /////Returns error: An object reference is required for the non-static field, method or property 'EnemyStats.AudioMain.get'
AudioClip Intro=(AudioClip)Resources.Load(AudioIntro, typeof(AudioClip));
AudioClip Loop=(AudioClip)Resources.Load(AudioMain, typeof(AudioClip));}
void PlayAudio(){
if (Intro == null)
AudioPlayer.clip = Loop;
AudioPlayer.loop = true;
AudioPlayer.Play ();
if (Intro != null)
AudioPlayer.clip = Intro;
AudioPlayer.loop = false;
AudioPlayer.Play ();
}}
As you can see my variables have getters and setters, if that makes a difference. Thanks in advance.
The last 2 lines should not have been in the script, that was an accident
Answer by jdean300 · Jul 08, 2016 at 09:07 PM
If it is non static, you need a reference. You'll only be able to set those variables inside of a function. If these components are both on the same object:
public class Battle : MonoBehaviour
{
//all you variables....
void Start()
{
EnemyStats e = GetComponent<EnemyStats>();
AudioIntro = e.AudioIntro;
AudioMain = e.AudioMain;
}
//your play audio function goes here
}
If they are on different object then you need to get a reference to the object that has the EnemyStats component, possibly through GameObject.Find(/*enemy object name*/)
and then call GetComponent<EnemyStats>()
on that object.
I don't know what to do then, because no game objects have the EnemyStats script on them.
If thats the case then how are you setting the AudioIntro and Audio$$anonymous$$ain variables in the EnemyStats script?
Through seperate scripts that are attatched to the enemies themseleves, but I guess I could combine it into one script.
But then there is an an object with the EnemyStats script...
Without the variables being static, you NEED to have the EnemyStats component attached to something, otherwise you'd have no way to get or set the variables
Okay, would it work if I attatched enemy stats to the player, then when they come in contact with an enemy, it finds the enemies stats in the enemystats script then starts a battle?
Ill send an example of one of the enemies scripts
using UnityEngine;
using System.Collections;
public class CWolfAlpha:EnemyStats{
public void CorruptWolfAlpha (){
EnemyName = "Corrupt Wolf Alpha";
EnemyDescription = "You have made a terrible choice.";
EnemyEncounterText = "You are going to regret this.";
EncryptFloat Level = new EncryptFloat(28);
EncryptFloat HP = new EncryptFloat(160);
EncryptFloat Attack = new EncryptFloat (17);
EncryptFloat Defense = new EncryptFloat(28);
EncryptFloat Speed = new EncryptFloat(17);
EncryptFloat Reward_EXP_$$anonymous$$in = new EncryptFloat(175);
EncryptFloat Reward_EXP_$$anonymous$$ax = new EncryptFloat(175);
EncryptFloat AI = new EncryptFloat(4);}}
The CorruptWolfAlpha IS an EnemyStats script. That's what inheritance does for you. So this would work:
public void SomeFunction()
{
GameObject wolf = GameObject.Find("WolfName");
EnemyStats e = wolf.GetComponent<EnemyStats>();
}
But the script that plays the music must work for every enemy, so could I have the individual enemies set the variables, the once it is set, have the battle system look at enemystats for the variables it needs?