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 /
  • Help Room /
avatar image
0
Question by ga3486u · Mar 29, 2020 at 04:38 AM · variablesreferenceinstance

Script that manages all the variables depending on the difficulty doesnt update on all the scripts using those variables.

Hello.

So I have a "GameManager" game object that has a script which has all the variables used throughout all the components and game objects of the game that change depending on the difficulty you select with a switch case in its Start() function (I haven't balanced the game yet as it doesn't work):

 public void Start()
 {
     difficulty = (int)GameDifficulty.Normal;
     switch (difficulty)
     {
         case 0: //Easy
             health = 100f;
             enemyHealth = 20f;
             fireRate = 0.2f;
             reloadRate = 1f;
             enemyMovementSpeed = 5f;
             bulletSpeed = 2f;
             invincibilityTimer = 1f;
             eSpawnTime = 5f;
             oSpawnTime = 10f;
             orbSpeed = 1f;
             LerpSpeed = 0.4f;
             break;
         case 1: //Medium
             health = 100f;
             enemyHealth = 20f;
             fireRate = 0.2f;
             reloadRate = 1f;
             enemyMovementSpeed = 5f;
             bulletSpeed = 2f;
             invincibilityTimer = 1f;
             eSpawnTime = 5f;
             oSpawnTime = 10f;
             orbSpeed = 1f;
             LerpSpeed = 0.4f;
             break;
         case 2: //Hard
             health = 100f;
             enemyHealth = 20f;
             fireRate = 0.2f;
             reloadRate = 1f;
             enemyMovementSpeed = 5f;
             bulletSpeed = 2f;
             invincibilityTimer = 1f;
             eSpawnTime = 5f;
             oSpawnTime = 10f;
             orbSpeed = 1f;
             LerpSpeed = 0.4f;
             break;
         case 3: //Pro
             health = 100f;
             enemyHealth = 20f;
             fireRate = 0.2f;
             reloadRate = 1f;
             enemyMovementSpeed = 5f;
             bulletSpeed = 2f;
             invincibilityTimer = 1f;
             eSpawnTime = 5f;
             oSpawnTime = 10f;
             orbSpeed = 1f;
             LerpSpeed = 0.4f;
             break;
     }

I cant seem to use any of the variables throughout the game even though I create instances of of this script.

For another example, I want to lerp my instantiated player object into the scene and once it reaches where I want it to reach I have a bool that tells another script to start spawning enemies and power ups:

 public void Update() //Player movement Update function
 {
     if (hasReached == false)
     {
         LerpValue += Time.deltaTime * 0.4f;
         transform.position = Vector3.Lerp(spawnPos, center, LerpValue);
         if (LerpValue > 1)
         {
             hasReached = true;
         }
     }
     movement.x = Input.GetAxis("Horizontal");
     movement.y = Input.GetAxis("Vertical");
 }

and:

 public void Update() //GameObjectSpawner Update function
 {
     if (pm.hasReached)
     {
         eSpawnTimer += Time.deltaTime;
         oSpawnTimer += Time.deltaTime;
         if (eSpawnTimer > GS.eSpawnTime)
         {
             EnemySpawner();
             eSpawnTimer = 0;
         }
         if (oSpawnTimer > GS.oSpawnTime)
         {
             OrbSpawner();
             oSpawnTimer = 0;
         }
     }
 }

I have made every gameobject a prefab to be able to reference them all in the inspector. I'm very new to coding and unity so I know a lot of what I'm doing is wrong. I'm just trying to figure out how to use these variables across each component. Here are some of the scripts to make more sense if I haven't already:

 public class GameObjectSpawner : MonoBehaviour //on the gamemanager prefab
 {
     public bool startGame, hasReached;
     private float eSpawnTimer, oSpawnTimer; //Enemy and Orb
     public Movement pm;
     public GameSettings GS;
     public GameObject playerPrefab, enemyPrefab, healthOrbPrefab, powerOrbPrefab, scoreOrbPrefab, deathOrbPrefab, invincOrbPrefab;
     public Vector3 spawnPos, enemySpawnPos, enemySpawnPoint1, enemySpawnPoint2, enemySpawnPoint3, enemySpawnPoint4, orbSpawnPos, orbEndPos, orbSpawnPoint1, orbSpawnPoint2, orbSpawnPoint3, orbEndPoint1, orbEndPoint2, orbEndPoint3;

     public void Start()
     {
         enemySpawnPoint1 = new Vector3(-2, 4, 0);
         enemySpawnPoint2 = new Vector3(2, 4, 0);
         enemySpawnPoint3 = new Vector3(-3, -4, 0);
         enemySpawnPoint4 = new Vector3(3, -4, 0);

         orbSpawnPoint1 = enemySpawnPoint1;
         orbSpawnPoint2 = new Vector3(0, 4, 0);
         orbSpawnPoint3 = enemySpawnPoint2;

         orbEndPoint1 = enemySpawnPoint3;
         orbEndPoint2 = new Vector3(0, -4, 0);
         orbEndPoint3 = enemySpawnPoint4;

         startGame = true;
         if (startGame)
         {
             spawnPos = orbEndPoint2;
             GameObject player = Instantiate(playerPrefab, spawnPos, Quaternion.identity);
         }
     }
     public void Update()
     {
         if (pm.hasReached)
         {
             eSpawnTimer += Time.deltaTime;
             oSpawnTimer += Time.deltaTime;
             if (eSpawnTimer > GS.eSpawnTime)
             {
                 EnemySpawner();
                 eSpawnTimer = 0;
             }
             if (oSpawnTimer > GS.oSpawnTime)
             {
                 OrbSpawner();
                 oSpawnTimer = 0;
             }
         }
     }

     void EnemySpawner()
     {
         int spawnPosition = Random.Range(0, 3);

         switch (spawnPosition)
         {
             case 0:
                 enemySpawnPos = enemySpawnPoint1;
                 break;
             case 1:
                 enemySpawnPos = enemySpawnPoint2;
                 break;
             case 2:
                 enemySpawnPos = enemySpawnPoint3;
                 break;
             case 3:
                 enemySpawnPos = enemySpawnPoint4;
                 break;
         }
         GameObject enemy = Instantiate(enemyPrefab, enemySpawnPos, Quaternion.identity);
     }
     void OrbSpawner()
     {
         int spawnPosition = Random.Range(0, 2);
         int orbSelector = Random.Range(0, 99);

         switch (spawnPosition)
         {
             case 0:
                 orbSpawnPos = orbSpawnPoint1;
                 orbEndPos = orbEndPoint1;
                 break;
             case 1:
                 orbSpawnPos = orbSpawnPoint2;
                 orbEndPos = orbEndPoint2;
                 break;
             case 2:
                 orbSpawnPos = orbSpawnPoint3;
                 orbEndPos = orbEndPoint3;
                 break;
         }
         if (orbSelector < 20) //all 20% chance for now
         {
             GameObject orb = Instantiate(powerOrbPrefab, orbSpawnPos, Quaternion.identity); //red
         }
         else if (orbSelector > 19 && orbSelector < 40)
         {
             GameObject orb = Instantiate(healthOrbPrefab, orbSpawnPos, Quaternion.identity); //green
         }
         else if (orbSelector > 39 && orbSelector < 60)
         {
             GameObject orb = Instantiate(deathOrbPrefab, orbSpawnPos, Quaternion.identity); //purple
         }
         else if (orbSelector > 59 && orbSelector < 80)
         {
             GameObject orb = Instantiate(invincOrbPrefab, orbSpawnPos, Quaternion.identity); //white
         }
         else
         {
             GameObject orb = Instantiate(scoreOrbPrefab, orbSpawnPos, Quaternion.identity); //gold
         }
     }
 }

 public class Movement : MonoBehaviour //on the player prefab
 {
     public float movementSpeed;
     private float LerpValue;
     public bool hasReached;
     private Vector3 spawnPos;
     public Vector3 center;
     public Rigidbody2D rb;
     private Vector2 movement;
     public AiHealth enemy;
     public OrbLogic orb;

     public void Start()
     {
         spawnPos = transform.position;
         center = new Vector3(0, -0.5f, 0);
     }

     public void Update()
     {
         if (hasReached == false)
         {
             LerpValue += Time.deltaTime * 0.4f;
             transform.position = Vector3.Lerp(spawnPos, center, LerpValue);
             if (LerpValue > 1)
             {
                 hasReached = true;
             }
         }
         movement.x = Input.GetAxis("Horizontal");
         movement.y = Input.GetAxis("Vertical");
     }

     private void FixedUpdate()
     {
         rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
     }

     private void OnCollisionEnter2D(Collision2D collision)
     {
         AiHealth enemyHit = collision.gameObject.GetComponent<AiHealth>();

         if (enemyHit != null)
         {
             enemyHit.ReceiveDamage();
         }
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         OrbLogic orbHit = collision.gameObject.GetComponent<OrbLogic>();

         if (orbHit != null)
         {
             orbHit.OrbActivation();
         }
     }
 }

I apologise for the wall of text and I appreciate anyone that read this far. Thanks for any help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Mar 29, 2020 at 05:14 AM

@ga3486u, I think I understand what you want to do, but I don't see where in your scripts you are doing it. Let me explain one way of doing it that is the easiest:

A) make sure you only have one instance of GameManager

B) create a static class, for example, GameVariables, with public static variables for each of your variables. Do not put this class as a script component on any game object - it just sits in your assets folder!

 //  for example:
 public static GameVariables {
          public static float health;
          public static float enemyHealth;
          public static float fireRate;
          public static float eSpawnTime;
          // and so on
  }

C) Where ever you want to reference any of these shared variables, preface it with the class name. For example:

    case 0: //Easy
          GameVariables.health = 100f;
          GameVariables.enemyHealth = 20f;
         // and so on ...
          break;

        // and
        GameVariables.eSpawnTime += Time.DeltaTime;


Remember that these are shared, so setting a GameVariables.variable value will make the change available everywhere you access the same GameVariables.variable

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

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

Object reference; sometimes works but sometimes doesn't??? 2 Answers

Help me to debug this error please :"NullReferenceException: Object reference not set to an instance of an object EnemyMovement.Update () (at Assets/Scripts/Enemy/EnemyMovement.cs:30)" 2 Answers

get all variables of a type from an object's components 0 Answers

c# Object reference not set to an instance of an object 1 Answer

How to Fix this issue 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