2017-03-28 32 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) 

或者,你可以给撞机到所有的形状和实际检查两个形状类型之间的冲突,可能使用层口罩,以保持从比较不同类型的形状。

我知道这并不能解释你所看到的所有行为,但这也许可以解释为什么它不会第一次增加你的分数。

由于您没有包含启用拖动的代码,因此我们无法知道这是否是问题。