子曦199107 子曦199107
关注数: 7 粉丝数: 3 发帖数: 71 关注贴吧数: 6
GetComponent的问题!!!!!! GetComponent放在update函数中有NullReferenceException: Object reference not set to an instance of an object的错误 kingController.cs文件 using UnityEngine; using System.Collections; public class kingController : MonoBehaviour { public float speed=3.0f; public Animator animator; CharacterController character; public bool attack=false; Vector3 Vspeed = Vector3.zero; CollisionFlags collisionFlags; public bool enemyAttack; GameObject obj; void Start () { animator = GetComponent<Animator> (); character = GetComponent<CharacterController> (); AttackTest obj = GetComponent<AttackTest> (); print (obj.attack.ToString()); enemyAttack = GetComponent<AttackTest> ().attack;//这句错误。。。。 } // Update is called once per frame void Update () { //print ("king "+obj.transform.position.ToString ()); enemyAttack = GetComponent<AttackTest> ().attack; 目的是从另一个脚本中获取一个bool类型的值。脚本名是AttackTest(作用是检测武器打击到敌人)这两个脚本是绑定在同一个物体上。(不在一个绑定在同一个物体上有影响吗?) AttackTest.cs文件 using UnityEngine; using System.Collections; public class AttackTest : MonoBehaviour { Vector3 startPosition; RaycastHit hitOut; Collider[] hit; public bool attack=false; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //attack = false; if (Input.GetMouseButtonDown (0)) { startPosition = transform.position; hit = Physics.OverlapSphere (startPosition, 1.0f, 1); { for(int i=0;i<=hit.Length-1;i++) { if(hit[i].tag=="Enemy") { //print ("attack!!!"); attack=true; //print(attack.ToString()); break; } } } } }
1 下一页