C# 将bool设置为具有1个脚本单位的不同对象
我在谷歌上读了很多页面,还有其他我能找到的东西,但没有任何东西对我有帮助,所以我不得不在这里问:我制作了一个附在100个立方体上的脚本,上面写着:C# 将bool设置为具有1个脚本单位的不同对象,c#,unity3d,C#,Unity3d,我在谷歌上读了很多页面,还有其他我能找到的东西,但没有任何东西对我有帮助,所以我不得不在这里问:我制作了一个附在100个立方体上的脚本,上面写着: using UnityEngine; using System.Collections; public class IfClose:MonoBehaviour{ public bool Gravitronned=false; // Use this for initialization void Start(){ } //
using UnityEngine;
using System.Collections;
public class IfClose:MonoBehaviour{
public bool Gravitronned=false;
// Use this for initialization
void Start(){
}
// Update is called once per frame
void Update(){
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.name =="IfInScreenGrv"){
Gravitronned=true;
}
else
{
Gravitronned=false;
}
}
}
然后我有另一个脚本,上面写着:
using UnityEngine;
using System.Collections;
public class TimeFreeze:MonoBehaviour{
static bool GravitronedTwice;
// Use this for initialization
void Start(){
}
void Update()
{
GravitronedTwice= gameObject.GetComponent<IfClose>().Gravitronned;
if(GravitronedTwice=true){
if(Input.GetKeyDown(KeyCode.V)){
Physics.gravity =newVector3(0,3.0F,0);
}
}
}
}
使用UnityEngine;
使用系统集合;
公共类时间冻结:单一行为{
静态布尔引力;
//用于初始化
void Start(){
}
无效更新()
{
GravitronedTwice=gameObject.GetComponent().Gravitronned;
if(GravitronedTwice=true){
if(Input.GetKeyDown(KeyCode.V)){
Physics.gravity=newVector3(0,3.0F,0);
}
}
}
}
所以当我按下V键时,我只希望这个区域的立方体得到Physics.gravity=newvector3(0,3.0F,0)代码>
使用GetComponent()调用第二个脚本中bool变量Gravitronned的值,这不是正确的方法
与其有两个bool变量,Gravitronned和Gravitronned两次,不如在第一个脚本中只有一个变量,将其公开(尽管公开是个坏主意,但这完全是另一个主题),并让它访问这两个脚本,而不是您正在做的事情。这样,您可以更改bool值,也可以在两个脚本中访问它
或者,您可以在脚本1中将bool-Gravitronned设置为私有,并通过C#属性在任何其他脚本中访问它。
脚本1
private bool Gravitronned;
public BOOL GRAVITY
{
get Gravitronned;
set Gravitronned;
}