简体   繁体   English

尝试从另一个脚本获取变量时使用未分配的局部变量

[英]Use of unassigned local variable when trying to get variable from another script

I'm trying to get a Float variable from the script MainCharacterVarsScript , I would use this Float in this current script. 我试图从脚本MainCharacterVarsScript中获取一个Float变量,我将在当前脚本中使用该Float。 The variable is called CharacterSpeed , both scripts are on the Game Object BaseCharacter . 该变量称为CharacterSpeed ,两个脚本都位于Game Object BaseCharacter上 Right now I have a kinda backwards way of doing it trying to remake it into a new variable on this script. 现在,我尝试将其重新制成此脚本中的新变量,这有一种向后的方式。 This is what I have currently, just me trying to get the component from the other script. 这是我目前拥有的,只是我试图从其他脚本中获取组件。 I am getting the error on line 15 (the long one) telling me that I am using an unassigned local variable. 我在第15行(较长的行)上收到错误,告诉我我正在使用未分配的局部变量。

Condensed version of what I want: 我想要的精简版:

To get a var from one script to another 从一个脚本到另一个脚本获取变量

1)Get float CharacterSpeed from MainCharacterVarsScript 1)从MainCharacterVarsScript获取float CharacterSpeed

2)Make it a usable float in my current script 2)在我当前的脚本中使其成为可用的浮动

3)Both on Game Object BaseCharacter 3)两者都在Game Object BaseCharacter上

Thanks for reading and helping 感谢您的阅读和帮助

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainCharacterMove : MonoBehaviour {


float MoveSpeed;

// Use this for initialization
void Start () {


    GameObject BaseCharacter = GameObject.Find ("BaseCharacter");
    MainCharacterVarsScript mainCharacterVarsScript = 
mainCharacterVarsScript.GetComponent<MainCharacterVarsScript>();
    mainCharacterVarsScript.CharacterSpeed = MoveSpeed;
}

// Update is called once per frame
void Update () {

}
}
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MainCharacterMove : MonoBehaviour
{    
    float MoveSpeed = 0f;

    // Use this for initialization
    void Start()
    {
        GameObject BaseCharacter = GameObject.Find("BaseCharacter");
        MainCharacterVarsScript mainCharacterVarsScript = mainCharacterVarsScript.GetComponent<MainCharacterVarsScript>();
        MoveSpeed = mainCharacterVarsScript.CharacterSpeed;
    }

    // Update is called once per frame
    void Update()
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM