Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 FeeeshMeister · Jul 08, 2016 at 09:01 PM · c#audiovariablesstringbattle-system

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.
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 FeeeshMeister · Jul 08, 2016 at 09:03 PM 0
Share

The last 2 lines should not have been in the script, that was an accident

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 11 · 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 FeeeshMeister · Jul 08, 2016 at 09:12 PM 0
Share

I don't know what to do then, because no game objects have the EnemyStats script on them.

avatar image jdean300 · Jul 08, 2016 at 09:13 PM 0
Share

If thats the case then how are you setting the AudioIntro and Audio$$anonymous$$ain variables in the EnemyStats script?

avatar image FeeeshMeister · Jul 08, 2016 at 09:16 PM 0
Share

Through seperate scripts that are attatched to the enemies themseleves, but I guess I could combine it into one script.

avatar image jdean300 FeeeshMeister · Jul 08, 2016 at 09:17 PM 0
Share

But then there is an an object with the EnemyStats script...

avatar image jdean300 FeeeshMeister · Jul 08, 2016 at 09:18 PM 0
Share

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

avatar image FeeeshMeister jdean300 · Jul 08, 2016 at 09:20 PM 0
Share

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?

avatar image FeeeshMeister · Jul 08, 2016 at 09:19 PM 0
Share

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);}}
avatar image jdean300 FeeeshMeister · Jul 08, 2016 at 09:21 PM 0
Share

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>();
 }
avatar image FeeeshMeister jdean300 · Jul 08, 2016 at 09:24 PM 0
Share

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?

Show more comments

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

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

Related Questions

Enemy Variable Battle System 1 Answer

Unity noob here, need help adding footstep logic 1 Answer

getting variables from other scripts C# 1 Answer

Variable changing from other script does not work. 1 Answer

Check if 2 InputField contains 2 words (not the same)? Feel like it's easy but can't figure out what's wrong 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