2017-03-28 37 views
0

我正在做一個簡單的拖放統一遊戲。當我第一次拖動對象時,它不會給分,但是當我再次拖動對象時,即使我在錯誤的地方掉下來,也會給分。我使用的標籤,以匹配所需的對象在統一標籤中找不到第一次拖動

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using System; 

public class sh_score : MonoBehaviour {  
    static int score = 0; 
    public Text scoreText; 

    public GameObject ans_circle; 
    public GameObject tag_circle; 
    public GameObject tag_rectangle; 
    public GameObject ans_rectangle; 
    public GameObject ans_triangle; 
    public GameObject tag_triangle; 
    public GameObject ans_square; 
    public GameObject tag_square; 
    public GameObject ans_star; 
    public GameObject tag_star; 

    void Start() 
    { 
     score = score; 

     if((ans_circle == null || tag_circle == null) || (ans_rectangle == null || tag_rectangle == null)|| (ans_square == null || tag_square == null) || (ans_triangle == null || tag_triangle == null)|| (ans_star == null || tag_star == null)) 
     { 
      ans_circle = GameObject.FindGameObjectWithTag("ans_circle"); 
      if (ans_circle != null) 
      { Debug.Log("ans Find"); } 

      tag_circle = GameObject.FindGameObjectWithTag("circle"); 
      if (tag_circle != null) 
      { 
       Debug.Log("circle"); 
      } 

      checkTagPlace(); 

      ans_rectangle = GameObject.FindGameObjectWithTag("ans_rectangle"); 
      if (ans_rectangle != null) 
      { Debug.Log("ans Find"); } 

      tag_rectangle = GameObject.FindGameObjectWithTag("rectangle"); 
      if (tag_circle != null) 
      { 
       Debug.Log("rectangle"); 
      } 

      ans_triangle = GameObject.FindGameObjectWithTag("ans_triangle"); 
      if (ans_triangle != null) 
      { Debug.Log("ans Find"); } 

      tag_triangle = GameObject.FindGameObjectWithTag("triangle"); 
      if (tag_triangle != null) 
      { 
       Debug.Log("triangle"); 
      } 

      ans_star = GameObject.FindGameObjectWithTag("ans_star"); 
      if (ans_star != null) 
      { Debug.Log("ans Find"); } 

      tag_star = GameObject.FindGameObjectWithTag("star"); 
      if (tag_star != null) 
      { 
       Debug.Log("star"); 
      } 

      ans_square = GameObject.FindGameObjectWithTag("ans_square"); 
      if (ans_square != null) 
      { Debug.Log("ans Find"); } 

      tag_square = GameObject.FindGameObjectWithTag("square"); 
      if (tag_square != null) 
      { 
       Debug.Log("square"); 
      } 
     }  
    } 

    void update() 
    { 
     scoreText.text = score.ToString(); 
    } 

    public void IncrementScore() 
    {  
     score = score + 9; 
     score++; 
     scoreText.text = "Score: " + score; 
    } 

    public void checkTagPlace() 
    { 
     Debug.Log("check function run"); 
     float circle_postion = tag_circle.transform.position.x; 
     float circle_Tag_positon = ans_circle.transform.position.x; 
     float triangle_position = tag_triangle.transform.position.x; 
     float triangle_Tag_positon = ans_triangle.transform.position.x; 
     float square_postion = tag_square.transform.position.x; 
     float square_Tag_positon = ans_square.transform.position.x; 
     float star_postion = tag_star.transform.position.x; 
     float star_Tag_positon = ans_star.transform.position.x; 
     float rectangle_position = tag_rectangle.transform.position.x; 
     float rectangle_Tag_positon = ans_rectangle.transform.position.x; 
     if ((ans_circle.transform.position.x == tag_circle.transform.position.x)) 
     { 
      Debug.Log("found position"); 
      IncrementScore();  
     } 
     else if ((ans_rectangle.transform.position.x == tag_rectangle.transform.position.x)) 
     { 
      IncrementScore(); 
     } 
     else if ((ans_square.transform.position.x == tag_square.transform.position.x)) 
     { 
      IncrementScore(); 
     } 
     else if (ans_triangle.transform.position.x == tag_triangle.transform.position.x) 
     { 
      IncrementScore(); 
     } 

     else if (ans_star.transform.position.x == tag_star.transform.position.x) 
     { 
      IncrementScore(); 
     } 
    } 
} 
+0

無效更新()需要以大寫字母U – JeanLuc

+0

仍然相同的錯誤 – jiya

回答

3

我還不能發表評論,但

  • 要調用checkTagPlace之前將其具有所有屬性也可能是錯誤的原因。
  • 搜索與標籤gameObjects如果你有相同的標籤
  • 通常幾個對象,做你想做的事,你想用對撞機上的遊戲物體和OnCollisionEnter/OnCollisionStay /什麼這樣可能會在未來的一個問題OnTriggerEnter/OnTriggerStay函數(不要忘記大寫)。然後,您將能夠檢查「碰撞」的GameObject是否具有正確的標記。
+0

因爲您的第一個bulletpoint給了你一個upvote,因爲這是一個答案。它可能不是正確的,但它是一個答案。 – krillgar

1

我相信你的問題可能是在這裏:

if(ans_rectangle.transform.position.x == tag_rectangle.transform.position.x) 

當你檢查,看看是否這些位置(以及其他,如果像這樣的語句)是平等的,你正在檢查,如果他們正好是相等。除非你有一些控制器正在離散你的形狀的運動,那幾乎不會發生。

我相信你想要的東西是真的是這樣的:

float epsilon=.001; 
if(Math.abs(ans_rectangle.transform.position.x - tag_rectangle.transform.position.x)<epsilon) 

或者,你可以給撞機到所有的形狀和實際檢查兩個形狀類型之間的衝突,可能使用層口罩,以保持從比較不同類型的形狀。

我知道這並不能解釋你所看到的所有行爲,但這也許可以解釋爲什麼它不會第一次增加你的分數。

由於您沒有包含啓用拖動的代碼,因此我們無法知道這是否是問題。