Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Public Static Variables out of context?

Discussion in 'Scripting' started by idrog, Aug 10, 2016.

  1. idrog

    idrog

    Joined:
    Jul 30, 2016
    Posts:
    83
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Master : MonoBehaviour
    6. {
    7.  
    8.     public GameObject OC;
    9.    public static float totalz = 0f;
    10.  
    11.  
    12.  
    13.     IEnumerator OnMouseEnter()
    14.     {
    15.         if (totalz == 0f)
    16.         {
    17.             totalz = 1f;
    18.             OC.SetActive(false);
    19.            
    20.  
    21.             yield return null;
    22.  
    23.         }
    24.     }
    25. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Master2 : MonoBehaviour {
    5.  
    6.    
    7.         public GameObject OG;
    8.    
    9.    
    10.  
    11.     IEnumerator OnMouseEnter()
    12.     {
    13.      
    14.        
    15.         if (totalz == 1f)
    16.         {
    17.             totalz = 2f;
    18.             OG.SetActive(false);
    19.          
    20.  
    21.             yield return null;
    22.  
    23.         }
    24.     }
    25. }
     
  2. idrog

    idrog

    Joined:
    Jul 30, 2016
    Posts:
    83
    Hello all! I have 2 OnMouseEnter functions attached to two different game objects. If the global static variable "totalz" is equal to 0f, I want the Gameobject that the mouse has entereted too disappear. If totalz == 1, then I want the other game Object to disappear. The reason I am using totalz, is so I Can control the order of when the Mouse enters each object. But on my second script, I keep getting, totalz does not exist in the current context? Why so, when I have declared it public and static? What am I Doing wrong?
     
  3. xexuxjy

    xexuxjy

    Joined:
    Feb 10, 2014
    Posts:
    18
    You'd need to refer to it as Master.totalz as that's the class the static belongs to.
     
    idrog likes this.
  4. idrog

    idrog

    Joined:
    Jul 30, 2016
    Posts:
    83
    Yea thanks xex! I'm stupid, I got that right now too by rewatching "statics" the video.