GameObject无法找到findgameobjectwithtag

问题描述 投票:0回答:2

我有点困惑,为什么会这样,所以基本上我试图让它到相机跟随玩家的地方,而不是左右移动他。

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;

    }
}
c# unity3d monodevelop
2个回答
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 () {

    }
}

2
投票

我想你想要GameObject.FindWithTag https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

即使是这样,

GameObject.FindWithTag ("Player").transform;

什么也没做。 (至少没用)

lookAt永远不会被分配,所以我猜你想做什么

lookAt = GameObject.FindWithTag ("Player").transform;
© www.soinside.com 2019 - 2024. All rights reserved.