This question has already been answered:

Good day. There are 2 .cs scripts. The first script displays a brief information about the task:

public bool quest1; public Text quest1Text; public void Start() { quest1 = false; quest1Text.text = ""; } public void OnCLick_Button() { quest1 = true; if (quest1) { Debug.Log("Задание взято"); quest1Text.text = "Например - что бы выполнить задание, необходимо прочитать книгу."; } } 

The second script, as planned, should change the Quest1 boolean variable:

 public void OnMouseDown() { AcceptQuest.quest1 = false; } 

If I make variables static in the first file, then the Boolean and text variables will not be displayed in the object inspector. Ie when I start, I get a warning that there are no objects to show. The question is, how do I transfer the value from one script to another?

Thanks for the help, Amateur.

Reported as a duplicate by participants Alexey Shimansky , br3t , Cheg , Lex Hobbit , Pavel Mayorov c # Aug 15 '17 at 12:08

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • And what does the inspector mean? When run in the editor, static variables are not written, but they are. - pavel
  • But there are lots of ways ....... for example through var go = GameObject.Find("someGameObjectOnScene").gameObject.GetComponent<YourComponent>() and manipulate through go ... go.quest1 = false/true; ..... And if the static variables, then immediately var questVar = Somescript.quest1; ............ and in the inspector are not displayed because static variables belong to the class and not to the object .... and you can throw a component on many prefabs ... ... and as in In this case, the unit must resolve the situation and at the same time so that you do not get confused that this variable is a class and not an object? - Alexey Shimansky
  • I could not figure out why using the static modifier declared variables disappear from the inspector, but the point is that the variables work even in this case. Therefore, I just added task status statuses (a regular int variable) and was able to solve the problem with getting, executing and returning the task. - Amateur
  • one
    I wrote to you why .... a static variable is not tied to an object, it refers only to the class so it cannot be displayed in the inspector .... For there may be a confusion that you will change it for each object in the inspector, in hope that it will have its own for each object, but in the end it will change for all objects .... It is logical that in order to avoid such jambs it is not displayed there ......... you should read all that is static variables to more fully understand why this is happening - Alex Shiman cue
  • I decided to make as a separate question ru.stackoverflow.com/questions/513204/… ...... there is a solution through SuctomEditor .... what are you looking for ... Only this is a crutch rather than a solution ... good luck with development - Alexey Shimansky

1 answer 1

The ability to display static objects in the inspector is not provided from the box. Although I saw somewhere a crutch as you can display, but there you need to edit the script files of the unit itself. ( https://docs.unity3d.com/ru/current/Manual/editor-CustomEditors.html ) And this is considered bad form.

The question is, how do I transfer the value from one script to another?

There are 3 ways.

  1. In the necessary object you create an instance of the script from the test 1. And then in the inspector you drag the object with the script hung on it in the corresponding field

  2. In Start, find the desired object by name and retrieve the necessary component `GameObject.Find (" name ") from it. GetComponent <ScriptNameIzCitates1> ()

  3. Learn what is singleton