GameObject无法找到findgameobjectwithtag

时间:2018-06-28 17:51:56

标签: c# unity3d monodevelop

为什么会这样,我有点困惑,所以基本上我只是想让它到达摄像机跟随玩家的位置,而不用左右移动。

using System.Collections;
using UnityEngine;

public class CameraMotor : MonoBehaviour {

    private Transform lookAt;
    private Vector3 startOffset;
    private Vector3 moveVector; 

    void Start () {
        GameObject.FindGameObjectWithTag ("Player").transform;
    }

    // Update is called once per frame
    void Update () {
        moveVector = lookAt.position + startOffset;

        //X
        moveVector.x = 0; //center of track
        //Y[image][1]
        moveVector.y = Mathf.Clamp(moveVector.y,3,5);// for ramps/stairs
            transform.position = moveVector;

    }
}

2 个答案:

答案 0 :(得分:2)

我认为您想要GameObject.FindWithTag https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

即使如此,

GameObject.FindWithTag ("Player").transform;

什么都不做。 (至少没有用)

lookAt从未分配,所以我猜您想做什么

lookAt = GameObject.FindWithTag ("Player").transform;

答案 1 :(得分:1)

If you are looking for an object in your scene, then use this simple script;

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

public class AnswerScript : MonoBehaviour {


    private Transform lookAt;


    // Use this for initialization
    void Start () {
        lookAt.Find("The Object That You are Looking For");
    }

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

    }
}