Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Amakuza · Mar 22, 2012 at 06:57 PM · c#variableinstancecs

C# Instance problem

Hello everyone in the community! i just would like to ask if theres a problem in instance accessing in c#. I have created several c# scripts: the first is the main where there is the oncollider script on it and some ongui functions which primary purpose is to make a quiz game.

The scripts are working great until i created 100's of it (one question = one script). So i arranged my scripts into folders(group by subject of quiz).

To make it work i drag 7 scripts into a box where they see each other, to make the instance thing work and it worked. But later on i decided to add a sound each time the player clicks on the correct answer and i don't want to hugely edit each script by adding variables and if statements for audio playing. So i decided create one c# script with a var of boolean value and if that boolean value is true it will play a sound.

The sound script to one of the boxes and edit the other quiz scripts and i added a line like this QuizeSound.Instance.Sounder=true; so everytime i clicked on the right answer in the quiz i will hear a sound. Working on the first box as i tested it i attached it to other quiz boxes so it will work too. Unfortunately after editing the scripts, it doesn't appear to read the variable of the sound script. I run the game and monitored the boolean variable in runtime and its always false like theres no connection at all even the script is attached and theres a QuizeSound.Instance.Sounder=true in each script.

Is this a bug or is this normal that you can only use instance in one object because when i created another sound script instance(just renamed the class and the instance) it worked.

To clarify it more it looks like this:

Box 1 scripts: quiz1, quiz2, quiz3, quiz4, quiz5, sounder Box2 2 sciprts: quiza, quiz2a, quiz3a, quiz4a, quiz5a, sounder

The box 1 works but the box 2 doesn't.

Then i do this:

Box 1 scripts: quiz1, quiz2, quiz3, quiz4, quiz5, sounder Box2 2 sciprts: quiza, quiz2a, quiz3a, quiz4a, quiz5a, sounder2

and it worked. I don't know whats wrong in solution 1 but theres no compiling error there.

using UnityEngine; using System.Collections;

public class QuizeSound : MonoBehaviour { public static QuizeSound Instance; public AudioClip impact;

 public bool Sounder =false;
 
 public int answered;
 
 void Start () 
 {
     
     Instance = this;
 }
 
 
     
 void Update () 
 {
     
 
     
   
     if (Sounder == true)
     {
         
             
     

      
         
         audio.PlayOneShot(impact);
         Sounder= false;
         
                                                     
         
         
         
         
 
 }

} }

Above is the sounder instance code and below is a quiz script sample that is accessing it: using UnityEngine; using System.Collections;

public class Quize73D2 : MonoBehaviour { public static Quize73D2 Instance;

 public int activeDistance = 10;

 public Transform Player;

public GUISkin customSkin; public static int answered = 0;

 void Start()
 {
     Instance = this;
 }

  void OnGUI () {
       GUI.skin = customSkin;
    if (Vector3.Distance(transform.position, Player.position) <= activeDistance)
    {
        GUI.Box(new Rect(350, 15, 800, 500), "What is the 3ds Max hotkey for rotate button?");
        
        
        
        // Make the first button. 
        if (GUI.Button(new Rect(500, 120, 500, 30), "R"))
        
        {
           

            QuizeController3D2.Instance.ShowQuize73D2 = false;
            QuizeController3D2.Instance.WonOrFailed3D2 = true;

        }
        
        // Make the second button.
        if (GUI.Button(new Rect(500, 200, 500, 30), "E"))
        
        {
             QuizeController3D2.Instance.answered++;QuizeSound.Instance.Sounder=true;
            QuizeController3D2.Instance.ShowQuize73D2 = false;
            QuizeController3D2.Instance.WonOrFailed3D2 = true;
            //One wrong answer  means fail and this is a wrong answer therefore display a "you are failed" message then exit
           
            
        }
        
        // Make the 3rd button.
        if (GUI.Button(new Rect(500, 280, 500, 30), "C"))
        {
            QuizeController3D2.Instance.ShowQuize73D2 = false;
            QuizeController3D2.Instance.WonOrFailed3D2 = true;
            //One wrong answer  means fail and this is a wrong answer therefore display a "you are failed" message then exit
        }
        
        
        
        // Make the 4th button.
        if (GUI.Button(new Rect(500, 360, 500, 30), "Alt + shift"))
        {
            QuizeController3D2.Instance.ShowQuize73D2 = false;
            QuizeController3D2.Instance.WonOrFailed3D2 = true;
            //One wrong answer  means fail and this is a wrong answer therefore display a "you are failed" message then exit
        }
        
        
        
        
        
        
        
    }
 }

}

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 DaveA · Mar 22, 2012 at 07:02 PM 0
Share

You should post your code (edit your question), and select it, and format it with the 010/101 button

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Mar 22, 2012 at 07:04 PM

Shooting blind: Sounds like you made a public var, added to game object, which takes the initial value you gave it in script. if you change the var's default in script, it is NOT automatically set to the game object. Check your game object to see if the bool is set right there. You can make it private if this bugs you.

Comment
Add comment · Show 1 · 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 Amakuza · Mar 22, 2012 at 08:20 PM 0
Share

Your right thats public but if i set it to private other scripts cant access it because of the protection level.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Variable Type for an Instance of any Script 2 Answers

Adding up instances of a variable across different objects? 1 Answer

Passing Object References Through Function 0 Answers

Updating variable on another script. It is not working. 2 Answers

Creating fields/instance variables 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