Enemy Animator unity3d(2d)返回动画师未初始化

时间:2014-10-30 02:10:15

标签: android unity3d unityscript

这是我的克隆(实例化)的敌人。我有一个错误,我认为我设置的一切都是正确的。

错误:

  

Animator尚未初始化。   UnityEngine.Animator:SetFloat(String,Single)enemyscript:playerhit()   (在Assets / enemies / enemyscript.js:14)

如何在每个克隆中初始化动画师。

#pragma strict
var enemysmall : GameObject;
var speed : float = 1.0;
var anim : Animator;

function Start() {
  this.transform.position.x = 8.325;
  this.transform.position.y = -1.2;
  anim = GetComponent(Animator);
  anim.SetFloat("enemy1sta",0);
}

function playerhit() {
  anim.SetFloat("enemy1sta",1);
}

function Update() {
  this.transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
  this.rigidbody2D.velocity = Vector2(-5, 0);
}

这是我在我的播放器上从预制件中生成它们

 var enemy1 : Transform;

 function Start () { 
 ///enemy1
   while (true) {
     yield WaitForSeconds (Random.Range(3, 5));
     var enemy1Clone : Transform = Instantiate(enemy1, transform.position, transform.rotation);
     enemy1Clone.GetComponent("enemyscript");
     enemy1Clone.GetComponent(Animator);
   }
}

我正在使用一个简单的

function OnCollisionEnter2D(coll: Collision2D) {
  if (coll.gameObject.CompareTag("enemy") && jumped==true){
    other.playerhit();   < ----- i have it so this calls the enemy function.
    //Destroy(coll.gameObject);
    jumpup(); 
    jumped = true;
    combo += 1;
    GUIcombo.text = "COMBO: " + combo;
  }
}

有人可以帮助我解决这个错误,它真的让我失去了我的第一个游戏开发

2 个答案:

答案 0 :(得分:1)

您正尝试将Components存储在同一个变量中。这是错的:

var enemy1Clone : Transform = Instantiate(enemy1, transform.position, transform.rotation);
enemy1Clone.GetComponent("enemyscript");
enemy1Clone.GetComponent(Animator);

enemy1Clone的类型为Transform,请勿尝试将enemyscriptAnimator组件放入其中。

你不需要那些GetComponent,所以只需丢弃它们。


我完全改变了游戏的逻辑。它现在有效。

<强> Player.js

function Awake()
{
    GUIcombo.text = "COMBO: ";
    GUIlives.text = "LIVES: 3";
}


function Start () 
{ 
    anim = GetComponent(Animator);
}


function Update () 
{
    if(lives <= 0)
        Destroy(Player);

    Player.transform.position.x = -4.325;

    if(!jumped)
        anim.SetFloat("hf",0.0);

    if(Input.GetButtonDown("Fire1") && !jumped)
    {
        jumpup();
        jumped = true;

        anim.SetFloat("hf",1);
    }
}


function OnCollisionEnter2D(coll: Collision2D) 
{
    var g : GameObject = coll.gameObject;

    if(g.CompareTag("ground"))
    {
        anim.SetFloat("hf",0.0);
        jumped=false;
        combo = 0;
        GUIcombo.text = "COMBO: " + combo;
    }

    else if(g.CompareTag("enemy") && jumped)
    {   
        // Notify the Enemy to die.
        g.SendMessage("die");

        jumpup(); 
        jumped=true;
        combo += 1;
        GUIcombo.text = "COMBO: " + combo;
    }

    else if(g.CompareTag("enemy") && !jumped)
    {
        lives -=1;
        GUIlives.text = "LIVES: " + lives;
    }
}


function slam()
{
    Player.rigidbody2D.AddForce(new Vector2(0,-3000), ForceMode2D.Force);
}

function glide()
{
    Player.rigidbody2D.AddForce(Vector2(0,600), ForceMode2D.Force);
}

function jumpup()
{
    Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
    Player.rigidbody2D.velocity = Vector2(0,10);

    if(jumplevel2)
        Player.rigidbody2D.velocity = Vector2(0,13);

    if(jumplevel3)
        Player.rigidbody2D.velocity = Vector2(0,16);
}

<强> Enemy.js

#pragma strict

var enemy : GameObject;
var speed : float = 1.0;
var anim : Animator;
var isDead : boolean = false;

function Start()
{
   anim = GetComponent(Animator);
   anim.SetFloat("EnemyDie", 0);
   enemy.transform.position.x = 8.325;
   enemy.transform.position.y = -1.2;
}

function Update()
{
    if(!isDead)
    {
        enemy.transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
        enemy.rigidbody2D.velocity = Vector2(-5, 0);
    }
}

// Call here when enemy should die. 
function die()
{
    if(!isDead)
    {
        isDead = true;
        anim.SetFloat("EnemyDie", 1);

        yield WaitForSeconds(0.5f);
        Destroy(gameObject);
    }
}

答案 1 :(得分:0)

如果未初始化 Animator ,则必须重新排序组件,将 Animator 组件放在转换组件的正下方,你应该没事。