【发布时间】: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