我的代码如下
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Utility;
public class CentipedeBoss : MonoBehaviour {
[System.Serializable]
public class Path
{
public BezierSpline spline;
public float duration;
}
public Path []path;
[System.Serializable]
public class SineWave
{
public float amplitude = 10.0f;
public float frequency = 1.0f;
}
public SineWave sineWave = new SineWave();
public float[] timers;
public float timer;
#region body parts
public GameObject head;
[System.Serializable]
public class Body
{
public GameObject []parts;
public float distance;
public float height;
public float rotationDamping;
public float heightDamping;
}
public Body body = new Body();
#endregion
SplineWalker currPathSpline;
int currPathIndex = 0;
public float duration;
void Start()
{
currPathSpline = head.GetComponent<SplineWalker>();
InitBodyParts();
InitTimer ();
}
void Update()
{
Movement (criticalPoints[0].bodyPart.transform);
duration -= Time.deltaTime;
if(duration <= 0)
{
ChangePath();
}
}
void InitTimer()
{
timers = new float[body.parts.Length];
for (int i = 0; i < timers.Length; i++)
timers [i] = timer;
}
[System.Serializable]
public class CriticalPoint
{
public GameObject bodyPart;
public bool state;
public CriticalPoint()
{
InitCriticalPoint();
}
private void InitCriticalPoint()
{
bodyPart = new GameObject();
state = true;
}
}
public CriticalPoint[] criticalPoints;
void InitBodyParts()
{
criticalPoints = new CriticalPoint[body.parts.Length];
Debug.Log ("size critpoints : " + criticalPoints.Length);
for(int i = 0; i < body.parts.Length; i++)
{
**criticalPoints[i].bodyPart = (GameObject) Instantiate(body.parts[i], Vector3.zero, new Quaternion(0,0,0,0));**
//bodyPart[i].transform.parent = transform;
criticalPoints[i].bodyPart.name = i + "";
criticalPoints[i].bodyPart.AddComponent<SmoothFollow>();
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().distance = body.distance;
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().height = body.height;
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().rotationDamping = body.rotationDamping;
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().heightDamping = body.heightDamping;
if(i == 0)
{
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().target = transform;
}
else
{
criticalPoints[i].bodyPart.GetComponent<SmoothFollow>().target = criticalPoints[i - 1].bodyPart.transform;
}
criticalPoints[i].state = true;
}
}
void ChangePath()
{
int rand = 0;
do
{
rand = UnityEngine.Random.Range(0, path.Length);
}
while(rand == currPathIndex);
currPathIndex = rand;
currPathSpline.spline = path[currPathIndex].spline;
duration = path[currPathIndex].duration;
currPathSpline.duration = duration;
}
void Movement(Transform body)
{
body.position += sineWave.amplitude*(Mathf.Sin(2*Mathf.PI*sineWave.frequency*Time.time) - Mathf.Sin(2*Mathf.PI*sineWave.frequency*(Time.time - Time.deltaTime)))*body.up;
//Debug.Log (body.position + " " + body.name);
}
public int CheckState()
{
bool alive = false;
int count = 0;
for (int i = 0; i < criticalPoints.Length; i++)
{
if (criticalPoints[i].state)
alive = true;
else
count++;
}
if (alive == false)
{
Destroy (gameObject);
}
return count;
}
}
Unity返回:NullReferenceException:未将对象引用设置为对象的实例 CentipedeBoss.InitBodyParts()(在Assets / Scripts / Centipede / CentipedeBoss.cs:136) CentipedeBoss.Start()(在Assets / Scripts / Centipede / CentipedeBoss.cs:53)
不起作用的行是:criticalPoints[i].bodyPart = (GameObject) Instantiate(body.parts[i], Vector3.zero, new Quaternion(0,0,0,0));
该行位于InitBodyPart()
函数中。