- Home /
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);
}
}
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090536im_/https://answers.unity.com/themes/thub/images/avi.jpg)