C# 实例化C时更改附加脚本中变量的值#

C# 实例化C时更改附加脚本中变量的值#,c#,unity3d,C#,Unity3d,我无法对我实例化的“子弹”应用任何东西,我无法访问它上的scipts。Instatitate之后,我希望附加到它的ScriptBulleter中的var的值为1 if (Input.GetMouseButton (1) && canFire) { var mousePosition = FindObjectOfType<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Inp

我无法对我实例化的“子弹”应用任何东西,我无法访问它上的scipts。Instatitate之后,我希望附加到它的ScriptBulleter中的var的值为1

if (Input.GetMouseButton (1) && canFire) {

    var mousePosition = FindObjectOfType<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - FindObjectOfType<Camera> ().transform.position.z));

    GetComponent<Rigidbody2D> ().transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x)) * Mathf.Rad2Deg - 90);

    bullet bulletobj = Instantiate (bulletFired, transform.position + transform.forward * 2, Quaternion.identity) as bullet;
    bulletobj.GetComponent<scriptbullet>().bulletDamages = 1; //this line doesn't work

    bulletobj.GetComponent<Rigidbody2D> ().velocity = (mousePosition - transform.position).normalized * bulletSpeed * Time.smoothDeltaTime;

    canFire = false;
}
if(Input.GetMouseButton(1)和&canFire){
var mousePosition=FindObjectOfType().ScreenToWorldPoint(新向量3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z-FindObjectOfType().transform.position.z));
GetComponent().transform.eulerAngles=新向量3(0,0,Mathf.Atan2((mousePosition.y-transform.position.y),(mousePosition.x-transform.position.x))*Mathf.Rad2Deg-90);
bullet bulletobj=实例化(bulletFired,transform.position+transform.forward*2,Quaternion.identity)为bullet;
bulletobj.GetComponent().BulletDemages=1;//此行不起作用
bulletobj.GetComponent().velocity=(mousePosition-transform.position)。规格化*bulletSpeed*Time.smoothDeltaTime;
canFire=假;
}

提前感谢各位:D

bulletobj.GetComponent()失败,因为
bulletobj
为空。从
实例化
返回的对象不是
项目符号
,而是
游戏对象
,请将该行代码更改为

GameObject bulletobj = Instantiate (bulletFired, transform.position + transform.forward * 2, Quaternion.identity) as GameObject;

将队列拆分,哪个部分失败,
bulletobj.GetComponent()
GetComponent().BulletDemages
,或者
BulletDemages=1
。根据失败的原因,这是一个不同的问题。第一个失败,它是一个附加到子弹上的脚本,而不是附加到射击者(游戏对象)上的脚本(第二个)…它是如何失败的,请复制并粘贴准确的错误文本到您的问题中。错误是“对象引用未设置为对象的实例”,但是有人已经把我带到了另一个无法解决我的问题的线程。哦,我想它现在起作用了。我会看看子弹是否击中了目标。如果它解决了你的问题,请务必接受答案。