【问题标题】:How can I change this value from a different script? UNITY C# [duplicate]如何从不同的脚本更改此值? UNITY C# [重复]
【发布时间】:2021-01-16 19:53:03
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Platform : MonoBehaviour
{
    public float speed = 10.0f;
    private Rigidbody2D rb;


    // Use this for initialization
    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        rb.velocity = new Vector2(-speed, 0);

    }

    // Update is called once per frame
    void Update()
    {
        //not important
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        //not important
    }
}

^^ 这是我的 Platform.cs

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

public class PlatformSpawner : MonoBehaviour
{
    public GameObject Platform;
    public GameObject Fireball;
    public float respawnTime = 0.1f;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(ObjectSpawning());
    }
    private void spawnPlatform()
    {
//not important
    }
    private void spawnFireball()
    {
//not important
    }
    IEnumerator ObjectSpawning()
    {
        while (true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnPlatform();
            spawnFireball();
            respawnTime *= 1.02f;
        }
    }
}

^^ 这是我的 PlatformSpawner.cs

我是 Unity 和 C# 的新手,如何从 PlatformSpawner.cs 更改 Platform.cs 中的 speed 值?我在网上查了一下,但我似乎找不到答案......所以我希望你们能帮助我! 我正在尝试逐渐增加速度值。 提前致谢(:

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您想只为一个平台更改速度,还是为所有平台更改?

    如果您希望所有平台的速度相同,则应将speed 设为静态。

    public static float speed = 10.0f;
    

    然后你可以这样调整速度。

    Platform.speed = 15.0f; //replace 15.0f with your desired speed.
    

    如果您想改变特定平台的速度,请从游戏对象中获取Platform 组件,然后修改速度。

    // assuming "platform" is of type gameObject
    platform.GetComponent<Platform>().speed = 15.0f; // replace 15.0f with your desired speed.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多