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 zcolek · Jan 07, 2013 at 01:59 PM · variablebuttonsmonobehaviourindexnullreference

Change variable in Mono from other script

Hello I have one problem... I have one game object and it contains only one sript, but in that script I have instances of other classes. I want to change some var int in realtime from other script, I have function for some button and on click I try to set var in mono to 1 but I get null reference... Also tryed instantiate a mono class and get warning that is not allowed...

for example:

This is the sctipt that is attached to empty game object...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class game : MonoBehaviour {
     public GUIStyle myStyle = new GUIStyle();
     public List<scene> sceneList = new List<scene>();
     public scene prva;
     public Texture2D mis;
     textures images;
     mainMenu mainManuScene;
     public int sceneIndex = 0;
 
     // Use this for initialization
     void Start () {
         mainManuScene = new mainMenu();
         mainManuScene.Awake();
         images = new textures();
         images.Awake();
         //Cursor.SetCursor(mis,Vector2.zero,CursorMode.Auto);
         prva = new scene(images.pozadina,mainManuScene.butList);
         sceneList.Add(prva);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnGUI(){
         switch(sceneIndex){
         case 0:
             mainManuScene.mainMenuFunction();
             break;
         default:
             break;
             
         }
             
     }
 }
 

And this is the other script where I have a function main which I call in game script, and I want to (you will see) on MouseUp where is commented Application.Quit() to set sceneIndex in game for example to 1...

notice mainMenuFunction.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class mainMenu{
     public List<buttons> butList = new List<buttons>();
     textures images;
     public void Awake(){
         images = new textures();
         images.Awake();
         buttons newGame = new buttons(images.newGameT,"ng",images.ngOver);
         buttons load = new buttons(images.loadT,"ld",images.ldOver);
         buttons options = new buttons(images.optionsT,"op",images.opOver);
         buttons credits = new buttons(images.creditsT,"cr",images.crOver);
         buttons quit = new buttons(images.quitT,"qt",images.qtOver);
         butList.Add(newGame);
         butList.Add(load);
         butList.Add(options);
         butList.Add(credits);
         butList.Add(quit); 
     }
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public void mainMenuFunction(){
         GUI.DrawTexture(new Rect(0f,0f,Screen.width,Screen.height),images.pozadina);
         for(int i = 0;i < butList.Count;i++){
             if(butList[i].butRect.Contains(Event.current.mousePosition)){
                 if(Event.current.type == EventType.MouseUp){
                     if(butList[i].name == "qt"){
                         //Application.Quit();
                     }
                 }
                 GUI.DrawTexture(butList[i].butRect,butList[i].imgOver);
             }else{
                 GUI.DrawTexture(butList[i].butRect,butList[i].img);    
             }
         }
         float x = (Screen.width/100f)*30f;
         float y = (Screen.height/100f)*2f;
         float width = (Screen.width/100f)*40f;
         float height = (Screen.height/100f)*25f;
         GUI.DrawTexture(new Rect(x,y,width,height),images.logo);
     }
 }
 
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 DanielJF · Jan 08, 2013 at 03:16 PM

I hope this is actualy what you need it's very basic but it shows you how to refference from 1 script to another. //script 1 public class TestFile1 : MonoBehaviour {

     public int SomeNumber = 1;
     public static TestFile1 main;
     
     
     void Awake () 
     {
         main = this;    
     }
 }

//Script 2

 using UnityEngine;
 using System.Collections;
 
 public class TestFile2 : MonoBehaviour {
 
     // Use this for initialization
     void Start () 
     {
         TestFile1.main.SomeNumber = 2;
     }
 }

I hope it helps you.

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

10 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

Related Questions

CSS -> z-index, GUI -> ? 3 Answers

AddListener with argument on multiple Buttons 1 Answer

Accessing a variable in one script from another script? (using C#) 1 Answer

DIALOGUE BRANCH CREATION? 1 Answer

accessing vars of other script doesn't work 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