【问题标题】:Unity Can not change the position of an objectunity 不能改变物体的位置
【发布时间】:2020-09-28 04:37:18
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnPoint : MonoBehaviour
{
    void Start()
    {
        print(this.transform.position);
        print(GameObject.Find("GameManager").GetComponent<GameManager>().player.transform.position);
        GameObject.Find("GameManager").GetComponent<GameManager>().player.transform.position = this.transform.position;
    }
}

此脚本位于我关卡中的游戏对象上。当打印返回生成点正确时,它会在关卡加载时运行。但是,玩家对象不会改变位置,它会停留在同一个位置。当场景发生变化时,第二个打印也可以正确返回玩家当前的位置。

我以前从未遇到过任何问题。我唯一能想到的是播放器在创建时设置为 DontDestroyOnLoad,但这应该不会影响它吧?

【问题讨论】:

  • 印刷品不会留下来。它只是用来测试值是否正确
  • 还有什么设置的位置吗?
  • 哦,等等,您可能正在做某事。播放器控制器可能会干扰生成更改。等我放学回来一定要检查一下

标签: c# unity3d


【解决方案1】:
void Awake()
    {
        GameManager gm = GameObject.Find("GameManager").GetComponent<GameManager>();
        GameObject player = gm.player;
        player.transform.position = this.transform.position;
    }

原来是因为场景还没有加载。我没有将 Start 改为 Awake,所以它等待加载所有场景对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多