当前位置:网站首页>Unity3d calls between different script functions or parameters
Unity3d calls between different script functions or parameters
2022-07-25 00:37:00 【AplusX】
If , We have two scripts :Main.cs,SliderControl.cs. Now hope from SliderControl.cs call Main.cs Function or parameter in .
( One )、 The called script function is static type , Call directly with Class name . Parameters
public class Main: MonoBehaviour {
public static int index = 0;
}
// stay SliderControl.cs Call in index
int para = Main.index;
( Two )、GameObject.Find(“ The name of the object to which the script is attached ”) Find the game object , Re pass GetComponent< Script name >(). Function name () Call the function in the script , Can only call public Type function
public class Main: MonoBehaviour {
public Vector3 CalculateTransPose(){
// Be careful : Must be public function
...
}
}
// stay SliderControl.cs Call in CalculateTransPose()
public class SliderControl : MonoBehaviour
{
private Slider silder;
public GameObject _GG1;
// Start is called before the first frame update
void Start()
{
silder = GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
silder.value += 0.1f * Time.deltaTime;
// call Main.cs Medium CalculateTransPose function
Vector3 func= _GG1.GetComponent<Main>().CalculateTransPose();
}
}
( 3、 ... and )、GameObject.Find(“ The name of the object where the script is located ”).SendMessage(“ Function name ”);( I haven't tried yet )
// Can call public and private Type function
It is said that , This is the early stage. unity How to provide , This method is out of date , Relatively low in efficiency , Therefore, it is no longer recommended here .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private void TakeDamage()
{
}
}
// Call... In other scripts TakeDamage() function
GameObject.Find("Enemy_1").SendMessage("TakeDamage", SendMessageOptions.DontRequireReceiver);
except SendMessage, also SendMessageUpwards and BroadcastMessage function , The parameters of the three functions are similar , Are all method names + Method parameters + Additional information options consist of .
SendMessageOptions
- SendMessageOptions.RequireReceiver // If the corresponding function is not found , Will report a mistake ( The default is this state )
- SendMessageOptions.DontRequireReceiver // Even if the corresponding function is not found , No errors reported , Automatically ignore
difference
SendMessage Push messages only to all scripts of the specified object
SendMessageUpwards Push messages to the specified object and all its parent objects
BroadcastMessage Push messages to the specified object and all its children
Be careful :
It seems that the object passed can only pass one variable at a time , Therefore, you can package multiple variables into an array for transmission .
边栏推荐
- Nodejs package
- [Bert] QA, reading comprehension, information retrieval
- @Mapkey usage instructions
- SQL file import database - Nanny level tutorial
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- Soft test --- fundamentals of programming language (Part 2)
- Promtool Check
- 数组中只出现一次的两个数字
- Principle of data proxy
- LeetCode_ 392_ Judgement subsequence
猜你喜欢

Implement a avatar looping control

Detailed explanation of alexnet of paddlepaddle paper series (with source code)

The model needs to use two losses_ FN, how to operate?

How to use measurement data to drive the improvement of code review
![[leetcode weekly replay] 303rd weekly 20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[leetcode weekly replay] 303rd weekly 20220724
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing weekly rematch] 61st weekly 20220723

paddlepaddle论文系列之Alexnet详解(附源码)

ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration
![[LeetCode周赛复盘] 第 83 场双周赛20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[LeetCode周赛复盘] 第 83 场双周赛20220723

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
随机推荐
What can testers do when there is an online bug?
Simple use of mongodb database
Chapter IV drive subsystem development
MySQL common basic commands
2012.4.13 360 written examination summary
Promtool Check
Install software on kubernetes cluster using helm 3 package manager
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
[mindspore] [training warning] warning when executing training code
如果实现与在线CAD图中的线段实时求交点
Solution to the shortest Hamilton path problem
[LeetCode周赛复盘] 第 303 场周赛20220724
What is the function of transdata operator and whether it can optimize performance
Quartus: install cyclone 10 LP device library for quartus version 17.1
Tencent low code platform is officially open source! You can drag and drop and generate mobile phone projects and PC projects! Get private benefits
Improve static loading dynamic loading
Deep and direct visual slam
px rem em
Measurement and Multisim Simulation of volt ampere characteristics of circuit components (engineering documents attached)
Redis pipeline technology / partition