提问者:小点点

团结一致该类不保存其方法中的字段值


我将Vector3参数从外部类传递给方法。然后将其分配到类字段。然后,当我想检查Update方法中的类字段值时,它说它是零。我做错了什么?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CustProjectile : MonoBehaviour
 {
 
     Vector3 direction;
     

     private void OnTriggerEnter(Collider other)
     {
         Destroy(gameObject);
     }
 
     public void InitiateProjectile(float speed, Vector3 direction)
     {
         Debug.Log(direction + "= PARAMETER FROM METHOD");
 
         this.speed = speed;
         this.direction = direction;
       
 
         Debug.Log(this.direction + "= VALUE IN THE CLASS");
     }
 
     private void Update()
     {
         Debug.Log(this.direction + "= VALUE IN THE CLASS FROM UPDATE METHOD");
         var dir2 = transform.position + direction;
         
         transform.position += dir2 * speed * Time.deltaTime;
       
     }
 
 }

这是传递Vector3参数的方法

public void AnimationShootProjectileAttack()
{

// projectile is a prefab

var bullet=Instantiate(projectile, transform.position+new Vector3(0,7f,0), Quaternion.identity);
var bullet_c = bullet.GetComponent<CustProjectile>();

projectileDirection= transform.TransformDirection(Vector3.forward);
bullet_c.InitiateProjectile(10f, projectileDirection);

}


共1个答案

匿名用户

我发现了问题所在。创建CustSpolder实例时,其属性向量3的编译晚于给出其值的方法。我不知道这是怎么可能的,但它是因为将没有领域给予价值,但…

事实上,我决定再次检查,只是为了确定我不能重现我在问题中的错误。嗯…