2016-05-04 4 views
-1
using UnityEngine; 
using System.Collections; 

public class Tree : MonoBehaviour { 

public int Health = 5; 
public Transform logs; 
public Transform coconut; 
public GameObject tree; 

public Camera myCamera; 

public int speed = 8; 

void Start() { 
    tree = this.gameObject; 
    GetComponent <Rigidbody>().isKinematic = true; 
    myCamera = GameObject.FindObjectOfType<Camera>(); 
} 

// Update is called once per frame 
void Update() { 
    if(Health > 0) 
    { 
     if(Vector3.Distance(transform.position, myCamera.transform.root.transform.position) < 10f) 
     { 
      if(Input.GetKeyDown(KeyCode.R) && WeaponSwitching.check == true) 
      { 
       Ray ray = new Ray(myCamera.transform.position,myCamera.transform.forward); 
       RaycastHit hit; 
       if(Physics.Raycast(ray,out hit,10f)) 
       { 
        if(hit.collider.gameObject == gameObject) 
        { 
         --Health; 
        } 
       } 
      } 
     } 
    } 

    if(Health <= 0) 
    { 
     GetComponent <Rigidbody>().isKinematic = false; 
     GetComponent <Rigidbody>().AddForce(transform.forward * speed); 
     DestroyTree(); 
    } 
} 

void DestroyTree() 
{ 

    wait(); 

Destroy(tree); 

Vector3 position = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0)); 
Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity); 
Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity); 
Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity); 

Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity); 
Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity); 
Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity); 
} 

IEnumerator wait() 
{ 
    yield return new WaitForSeconds (7.0f); 
} 
} 

オブジェクトをUnityEngine.Vector3に変換できないことを示しています。どのようにオブジェクトをVector3型に変換できますか?エラーはINSTANTIATEファンクションに表示されます。それはtree.transform.position + Vector3(0,0,0)+ positionがオブジェクト型であると言っています。どうすればこのエラーを修正できますか?エラーCS1503:引数 `#2 'は`オブジェクト式'を `UnityEngine.Vector3 'に変換できません。

答えて

2

あなたがココナッツをインスタンス化すると、その単純なタイプミスです。

Instantiate(coconut, tree,transform.position + Vector3(0,0,0) + position, Quaternion.identity);

も参照をキャッシュし、繰り返しGetComponentいけない

Instantiate(coconut, tree.transform.position + position, Quaternion.identity);

でなければなりません。しかし、私はコードを読み飛ばすだけでした。

編集: ああ、私はちょうどこれを読んでいる間、何かにVector3(0,0,0)を追加するのはかなり無意味です。何かに0を追加するなど、何もしません。

+0

どのような変更を加えましたか? –

+0

は2行を組み合わせます。あなたはプログラマーに計画しているときに一見するとそのような誤植を見つけることを学ぶことができます。そのようなことは、セミコロンがない場合のように共通しています。 – yes

+0

苦労している場合は、 'tree'の直後にカンマです。違いを見つけるのゲームを台無しにして申し訳ありません! –

3

まず、コードにコンパイル時エラーが発生した場合は、コードを書く前に停止して修正してください。コード内の他のエラーの上にエラーを作成することはできません。

コードにのエラーがありますが、間違いでこれらのエラーが生じました。

。あなたは、新しいベクトルを作成するためにnewキーワードを使用する必要があります。 1つの例外は、staticファンクションまたはコンストラクタをVector3.zero,Vector3.upなどのように呼び出す場合です。

もそう5つの他の過ちのためにこれを行いますtree.transform.position + new Vector3(0, 0, 0)

new Vector3(Random.Range.....

そしてtree.transform.position + Vector3(0, 0, 0)Vector3(Random.Range...を交換してください。

Vector3は、int,int,intではないパラメータとしてfloat,float,floatとなる。

この行のVector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0))には、floatの代わりにintVector3が渡されています。これを修正するには、Random.Range関数の各値の後にfと入力します。 fは、floatではなく、intであることをコンパイラーに通知します。あなたがしなければ。 float関数のオーバーロードの代わりにRandom.Range intのオーバーロードメソッドが呼び出されます。

Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));を再度new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));に変更してください。

public class Tree : MonoBehaviour 
{ 

    public int Health = 5; 
    public Transform logs; 
    public Transform coconut; 
    public GameObject tree; 

    public Camera myCamera; 

    public int speed = 8; 

    void Start() 
    { 
     tree = this.gameObject; 
     GetComponent<Rigidbody>().isKinematic = true; 
     myCamera = GameObject.FindObjectOfType<Camera>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Health > 0) 
     { 
      if (Vector3.Distance(transform.position, myCamera.transform.root.transform.position) < 10f) 
      { 
       if (Input.GetKeyDown(KeyCode.R) && WeaponSwitching.check == true) 
       { 
        Ray ray = new Ray(myCamera.transform.position, myCamera.transform.forward); 
        RaycastHit hit; 
        if (Physics.Raycast(ray, out hit, 10f)) 
        { 
         if (hit.collider.gameObject == gameObject) 
         { 
          --Health; 
         } 
        } 
       } 
      } 
     } 

     if (Health <= 0) 
     { 
      GetComponent<Rigidbody>().isKinematic = false; 
      GetComponent<Rigidbody>().AddForce(transform.forward * speed); 
      DestroyTree(); 
     } 
    } 

    void DestroyTree() 
    { 

     wait(); 

     Destroy(tree); 

     Vector3 position = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f)); 
     Instantiate(logs, tree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.identity); 
     Instantiate(logs, tree.transform.position + new Vector3(2, 2, 0) + position, Quaternion.identity); 
     Instantiate(logs, tree.transform.position + new Vector3(5, 5, 0) + position, Quaternion.identity); 

     Instantiate(coconut, tree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.identity); 
     Instantiate(coconut, tree.transform.position + new Vector3(2, 2, 0) + position, Quaternion.identity); 
     Instantiate(coconut, tree.transform.position + new Vector3(5, 5, 0) + position, Quaternion.identity); 
    } 

    IEnumerator wait() 
    { 
     yield return new WaitForSeconds(7.0f); 
    } 
} 
+0

私にはうわさがありません。タイトルに誤りがあるだけです。 (私はあなたが知っていることを確信しています) – yes

+0

@yesええ、私はintを入れています。なぜなら、random関数はそれがintだと思ってint return overloadを使うからです。それを見るだけで見つけるのは難しいので、恥ずべきことはありません。私はそれを見つけるためにファイルを作成しなければならなかった。 – Programmer

関連する問題