2016-12-02 111 views
-3

我是一个摧毁墙的临时文本玩家触发输入,但触发器只激活玩家将立方体放在相应的地方,如果立方体在正确的位置把比它会被销毁墙..摧毁一个墙c#unity3d

这是我的立方体 在这个脚本触发脚本就会破坏立方体和触发立方体 和墙壁马力的变量设置为0

using UnityEngine; 
using System.Collections; 

public class triggerwall : MonoBehaviour { 

public GameObject[] objects; 

void OnTriggerEnter(Collider other) 
{ 

    destroywall.hp -= 4; 
    Debug.Log ("hp wall"+destroywall.hp); 
    Destroy (gameObject); 
    Debug.Log ("game object"+gameObject); 
    Destroy (other.gameObject); 
    Debug.Log ("other game object"+other.gameObject); 
    //Destroy (gameObject); 
    //Destroy (other.gameObject); 



} 

// Finally, this line destroys the gameObject the player collided with. 
//Destroy(other.gameObject); 

} 

然后墙壁脚本 它会比较变量hp和hp x如果是==的破坏墙壁

以及我没有经验的C#开发人员,我有尝试多种形式,但我摧毁一切,但不是墙 这是墙脚本

using UnityEngine; 
using System.Collections; 


public class destroywall : MonoBehaviour 
{ 
        //Alternate sprite to display after Wall has been attacked by player. 
    public static int hp = 4;       //hit points for the wall. 
    public static int hpx = 0; 

void OnTriggerEnter(Collider other) { 
    if(hp == hpx) { 
     Debug.Log (other.gameObject.tag); 
     Destroy(other.gameObject); 



      } 

    } 
} 
+1

为什么hp是静态的?为什么你使用一个变量来存储0? –

+0

以及我不知道这么多从C#和我找到解决方案来比较两个变量来比较它们 –

+1

然后请记住,你只能有1个墙的实例。因为他们将共享相同的惠普。至少只要你保持静态。 –

回答

0

最后我已解决脚本通过更改墙脚本

using UnityEngine; 
using System.Collections; 


public class destroywall : MonoBehaviour 
{ 
        //Alternate sprite to display after Wall has been attacked by player. 
    public static int hp = 4;       //hit points for the wall. 
    public static int hpx = 0; 

void OnTriggerEnter ( Collider other ){ 
    if (other.tag == "destroywall") { 
     if (hp == hpx) { 
      Destroy (other.gameObject); 
     } 
    } 
} 


} 
+0

这是C#,如果有人需要知道,并坦克大家的帮助 –