与来自不同场景的对象交互 Unity 5

Interacting with objects from different Scenes Unity 5

下午好!我制作了一个主菜单,您可以在其中创建 RPG 角色,在这个菜单中,我可以从输入 fields/buttons 中获取值,从属性中获取 add/substract 值,等等。所有内容都存储在 class 称为 CustomCharacterSheet,现在这个对象有一个 DontDestroyOnLoad 脚本,允许它被移动到下一个场景,即第一级。

在我的 playerbehavior class 中,我有一个方法接受 CustomCharacterSheet 对象并读取它的值,然后创建一个具有适当值的角色,问题是当我想使用该方法生成playerbehavior 中 Awake() 上的字符,该方法需要一个 CustomCharacterSheet 类型的对象才能执行,但是我如何告诉该方法来自主菜单的 CustomCharacterSheet 是必须读取的对象?我试过 GameObject.Find();但它会告诉我它不能将 GameObject 转换为 CustomCharacterSheet class.

这是虚拟代码:

CustomCharacterSheetClass {
//Values, this was created into an object in the main menu with a DontDestroyOnLoad() script to be moved into the 1st Scene
}


PlayerBehavior Class {

private void generatePlayer(CustomCharacterSheet cs){
//Do Stuff, this method requires the CCS in order to pull data from it and generate the player
}

void Awake(){
generatePlayer();//This is inside the playerBehavior class and needs to reach the CustomCharacterClass created in the main menu that was later moved to the 1st Scene
}

}

GameObject.Find 用于按名称查找某个游戏对象,而不是用于查找脚本。

既然你说脚本附加到一个游戏对象,你需要做的就是用那个对象的名字调用 GameObject.Find("ObjectName") 然后你可以用 [=14] 访问脚本 CustomCharacterSheet =]

public class GetComponentGenericExample : MonoBehaviour
{
    void Start()
    {
        GameObject gObject = GameObject.Find("ObjectName")
        CustomCharacterSheet ccSheet = gObject.GetComponent<CustomCharacterSheet>();
    }
}

GetComponent 允许您访问游戏对象的任何组件。