Unity3D implementation calls functions and variables in other scripts


1. Demand target

  • When developing a Unity3D project, it is often necessary to call functions or variables defined in other scripts B in script A

Two, the solution

1. Create two empty game objects and two script files

insert image description here
Create two empty game objects, respectively GameObject_ScriptA, GameObject_ScriptB
insert image description here
Create two C# script files, respectively ScriptA, ScriptB

2. Write the script

  • What we need to achieve is to call the variables and functions defined in ScriptB in ScriptA
  • So you need to declare public functions and public variables in ScriptB first
  • The demo example used here is:
  • The public void DoSomething() function is used to display on the Console console: Here is script B;
  • public string ScriptB_String = "Hello World!" string;
  • Then declare an instance object of the ScriptB class in ScriptA UsingScriptB
  • Then write the functions and variables in ScriptB that need to be called in ScriptA

ScriptB code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptB : MonoBehaviour
{
    
    
    // 声明一个 public 的字符串 ScriptB_String, 内容为 Hello World!;
    public string ScriptB_String = "Hello World!";

    // 声明一个 public 的函数 DoSomething, 作用为在 Console 控制台上显示:Here is script B;
    public void DoSomething()
    {
    
    
        Debug.Log("Here is script B");
    }
}

ScriptA code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptA : MonoBehaviour
{
    
    
    // 声明一个 ScriptB 类的实例对象 UsingScriptB
    public ScriptB UsingScriptB;

    // Update is called once per frame
    void Update()
    {
    
    
        // 调用脚本 ScriptB 中的 DoSomething 函数
        UsingScriptB.DoSomething();
        // 显示脚本 ScriptB 中的 ScriptB_String 字符串变量
        Debug.Log("ScriptB_String: " + UsingScriptB.ScriptB_String);

        Debug.Log("************************************");
    }
}

3. Script link object

  • Link our ScriptA and ScriptB respectively (directly press and hold the script file and drag it to the game object) to the game objects GameObject_ScriptA and GameObject_ScriptB we created earlier
  • On GameObject_ScriptA we need to link the GameObject_ScriptB that has already linked the ScriptB script to our UsingScriptB object

insert image description here
insert image description here


3. Test results

insert image description here

  • After running the project, we can see the printed log information on the Console panel. The function call and variable printing here are all
    called in the Update() function in ScriptA, and the body of the function and variable is defined in ScriptB. and the declared
  • Thus perfectly realizing the functions and variables defined in our script A calling other script B
  • Of course, this is just a case demonstration. In this way, you can easily call functions, variables, etc. you want in various other scripts between different scripts in the Unity project...
  • This can not only increase the interoperability between scripts, but also save a lot of unnecessary repeated definitions

Good Luck !

Guess you like

Origin blog.csdn.net/weixin_46992165/article/details/126291624