C# 如何正确地公开变量,以便其他脚本可以访问它?
在Unity中,我有两个游戏对象,一个球体和一个胶囊 我每个人都有一个剧本 胶囊脚本:C# 如何正确地公开变量,以便其他脚本可以访问它?,c#,unity3d,private,public,C#,Unity3d,Private,Public,在Unity中,我有两个游戏对象,一个球体和一个胶囊 我每个人都有一个剧本 胶囊脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class CapsuleMesh : MonoBehaviour { public Mesh capsuleMesh; void Awake() { capsuleMesh = GetComp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh capsuleMesh;
void Awake()
{
capsuleMesh = GetComponent<MeshFilter>().mesh;
Debug.Log(capsuleMesh);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类胶囊:单一行为
{
公共网囊鱼;
无效唤醒()
{
capsuleMesh=GetComponent().mesh;
Log(capsuleMesh);
}
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
}
}
球体脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
Mesh mesh;
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
Debug.Log(mesh);
}
// Start is called before the first frame update
void Start()
{
mesh = capsuleMesh;
}
// Update is called once per frame
void Update()
{
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级变革:单一行为
{
网目;
无效唤醒()
{
mesh=GetComponent().mesh;
Log(mesh);
}
//在第一帧更新之前调用Start
void Start()
{
网格=胶囊网格;
}
//每帧调用一次更新
无效更新()
{
}
}
这里的mesh=capsuleMesh给了我一个关于“当前上下文中不存在名称capsuleMesh”的错误
我认为在另一个脚本中公开capsuleMesh将使这个脚本能够访问它而不会出现问题
我做错了什么
capsuleMesh
是在capsuleMesh
类中定义的类变量。它不是一个可以在任何地方使用的全局变量。您需要引用CapsuleMesh
类的实例来检索存储在CapsuleMesh
变量中的网格
我已经修改了你的两个脚本,使它们工作。我在你的剧本中发现了一个漏洞。我猜ChangeMesh
是为了改变游戏对象的网格?如果是这样,则需要为meshFilter.mesh
指定一个新值。为mesh
类变量指定一个新引用是不够的(解释原因可能需要很长时间)
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类胶囊:单一行为
{
公用网格
{
获得;私人设置;
}
无效唤醒()
{
Mesh=GetComponent().Mesh;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级变革:单一行为
{
//在检查器中拖放包含“CapsuleMesh”组件的游戏对象
公共胶囊;
私有网过滤器;
无效唤醒()
{
meshFilter=GetComponent();
}
void Start()
{
meshFilter.mesh=CapsuleMesh.mesh;
}
}
这就是解决方案。非常感谢。从现在起,我将遵循这种做法。我个人会用小写字母M命名属性mesh
,因为它不是一种方法(或者更确切地说,它看起来不像一种方法)。但是有人决定,因为属性是秘密的方法,所以它们应该遵循方法的格式规则。这一点特别重要,因为类名和属性名都是Mesh
,导致以后的用法不明确。我个人倾向于使用:与其他成员不同,属性应该使用名词短语或形容词名称。这是因为属性引用数据,而属性的名称反映了这一点。PascalCasing总是用于属性名。
但我完全同意类名和属性名精确匹配的问题。Microsoft建议这样做,尽管考虑为属性指定与其类型相同的名称。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh Mesh
{
get ; private set;
}
void Awake()
{
Mesh = GetComponent<MeshFilter>().mesh;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
// Drag & drop in the inspector the gameObject holding the `CapsuleMesh` component
public CapsuleMesh CapsuleMesh;
private MeshFilter meshFilter;
void Awake()
{
meshFilter = GetComponent<MeshFilter>();
}
void Start()
{
meshFilter.mesh = CapsuleMesh.Mesh;
}
}