简体   繁体   English

我放置的 Unity Prefab 不会移动

[英]Unity Prefab that i placed doesn't move

I want to move Prefab object so I write this code but when I place prefab on scene, it doesn't move.我想移动预制件 object 所以我写了这段代码,但是当我将预制件放在场景中时,它不会移动。 Only prefab in files x is changing.只有文件 x 中的预制件在变化。

What should I have to do?我应该怎么做? (This is 2D game.) (这是 2D 游戏。)

public IEnumerator IfButtonTouched() {

  Debug.Log("isWorking?");
  while (timer <= seconds) {

    timer += Time.deltaTime;

    obstacle.transform.Translate((new Vector3( - 1, 0, 0)) * speed * Time.deltaTime);

    Debug.Log("pleaseWork");
    yield
    return null;
  }

}

You can't access to a prefab with the name of the GameObject.您无法访问具有 GameObject 名称的预制件。

You cant Instantiate(obstacle, transform.position, quaternion.identity) and then你不能Instantiate(obstacle, transform.position, quaternion.identity)然后

obstacle.transform.Translate

Of course you will move the prefab and not the actual object!当然,您将移动预制件而不是实际对象!

You should do this way你应该这样做

GameObject obstacleSpawned = Instantiate(obstacle, transform.position, Quaternion.Identity) as GameObject;

And then you can access to obstacleSpawned and move it!然后你可以访问obstacleSpawned并移动它!

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

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