【问题标题】:How to make a button change a variable in a script then take that script into another script如何使按钮更改脚本中的变量然后将该脚本放入另一个脚本
【发布时间】:2020-02-16 15:20:21
【问题描述】:

所以我想在一个脚本中制作一个按钮来更改变量。然后我想要另一个脚本从第一个脚本中获取该变量。我该怎么做。

我已经尝试了一些东西,但我不确定我是否正确

当我在下面的代码中尝试它时。然后它给了我信息:

'找不到类型或命名空间名称'jumpscript'(是否缺少 using 指令或程序集引用?)'

Jump.cs

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

public class Jump : MonoBehaviour
{
    public bool jump = false;
    // Start is called before the first frame update

    public void JumpOnClick()
    {
        jump = true;
    }

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

    }
}

PlayerMovement.cs

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController2D controller;
    public Joystick joystick;
    public jumpscript Jump;

    public float runSpeed = 40f;

    float horizontalMove = 0f;

    // Update is called once per frame
    void Update()
    {
        if (joystick.Horizontal >= .2f)
        {
            horizontalMove = runSpeed;
        } else if (joystick.Horizontal <= -.2f)

        {
            horizontalMove = -runSpeed;
        } else
        {
            horizontalMove = 0f;
        }
    }

    void FixedUpdate()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, false, Jump.jump);
        Jump.jump = false;
    }
}

编辑:我让角色跳跃,但角色总是在跳跃。

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController2D controller;
    public Joystick joystick;
    public Jump jumpScript;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool Jump = false;

    // Update is called once per frame
    void Update()
    {
        if (jumpScript.jump == true)
        {
            Jump = true;
        }

        if (joystick.Horizontal >= .2f)
        {
            horizontalMove = runSpeed;
        } else if (joystick.Horizontal <= -.2f)

        {
            horizontalMove = -runSpeed;
        } else
        {
            horizontalMove = 0f;
        }
    }

    void FixedUpdate()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, false, Jump);
        Jump = false;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour
{
    public bool jump = false;

    // Start is called before the first frame update


    public void JumpOnClick()
    {
        jump = true;
        jump = false;
    }

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

    }
}

请帮帮我。

【问题讨论】:

    标签: c# unity3d computer-science


    【解决方案1】:

    好的,首先要注意的是,我假设这两个脚本都在一个游戏对象上。

    您必须从跳转脚本中创建一个局部变量。你可以这样完成它:

    private Jump jumpScript;
    
    // Start is called before the first frame update
    void Start()
    {
        jump = this.GetComponent<Jump>();
        jump.JumpOnClick();
    }
    

    【讨论】:

    • 类、结构或接口成员声明中的无效标记 '=' 无效标记 ';'在类、结构或接口成员声明中 无效标记“(”在类、结构或接口成员声明中 类型或命名空间定义,或文件结尾预期的无效标记“{”在类、结构或接口成员声明中
    • 你能解释一下我应该把它放在哪里
    • 所以我认为该代码块应该进入您的 PlayerMovement 类,因为这是您希望从中调用公共方法的类。我提供的示例有点通用,您可能需要针对您的用例进行更改。
    • 您遇到了问题,因为您将变量声明为Jump jumpScript,但您尝试分配jump = this.GetComponent&lt;Jump&gt;()
    猜你喜欢
    • 2020-12-09
    • 2017-11-07
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多