[英]Unity3D - get component
What is the simplest way to getcomponent in Unity3D C#
? 在Unity3D
C#
获取组件的最简单方法是什么?
My case: 我的情况:
GameObject gamemaster.
//C# script MainGameLogic.cs(attached to gamemaster).
A boolean backfacedisplayed(in MainGameLogic.cs).
A function BackfaceDisplay()(in MainGameLogic.cs).
Another C#
script FlipMech.cs
, which I need to check from MainGameLogic.cs
if(backfacedisplayed == TRUE)
, I will call BackfaceDisplay()
from MainGameLogic.cs
. 另一个
C#
脚本FlipMech.cs
,我需要从MainGameLogic.cs
检查if(backfacedisplayed == TRUE)
,我将从MainGameLogic.cs
调用MainGameLogic.cs
BackfaceDisplay()
。 How can I do it in C#? 我怎么能在C#中做到这一点?
In js is rather straight forward. 在js是相当直接的。 In
FlipMech.js
: 在
FlipMech.js
:
//declare gamemaster
var gamemaster:GameObject;
Then wherever I need: 然后我需要的地方:
if(gamemaster.GetComponent(MainGameLogic).backfacedisplayed==true)
{
gamemaster.GetComponent(MainGameLogic).BackfaceDisplay();
}
But it seems like C#
is way more complicated that this. 但似乎
C#
比这复杂得多。
in c#, you will get the component using this, 在c#中,你将获得使用它的组件,
if(gamemaster.GetComponent<MainGameLogic>().backfacedisplayed==true)
{
gamemaster.GetComponent<MainGameLogic>().BackfaceDisplay();
}
Nick's answer didn't work for me when I countered this same problem. 当我反驳同样的问题时, 尼克的回答对我不起作用。 The script involved an
Unity3d
asset store that has its own namespace. 该脚本涉及具有自己的命名空间的
Unity3d
资产商店。 When I put the class in the namespace, it started working. 当我把类放在命名空间中时,它开始工作。
using UnityEngine;
using System.Collections;
using Devdog.InventorySystem;
namespace Devdog.InventorySystem.Demo
{
public class changeStat : MonoBehaviour {
// ... rest of the code including the gameObject.GetComponent<>();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.