0

[编辑]

移动 [inject] 标签不起作用 //<--因为是 [Inject]

现在它有两个错误:

Assets\player\playercontroller\armory\rifle.cs(10,10):错误 CS0246:找不到类型或命名空间名称“injectAttribute”(您是否缺少 using 指令或程序集引用?)

Assets\player\playercontroller\armory\rifle.cs(10,10):错误 CS0246:找不到类型或命名空间名称“inject”(您是否缺少 using 指令或程序集引用?)

新代码在这里。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;

namespace zenject.injection
{ 
    public class InjectObject
    {
        [inject] //<is [Inject],fu
        GameObject RifleMuzzle;
    }

 
public class rifle : Weapon
{
   
   // Start is called before the first frame update
    private float cooldownTime = 0f;
    private float mag = 5;
    private int magSize = 5;
    
    public void Rifle() 
    {
        
    }
       
    

    public void Cooldown( float time ) 
    {
        cooldownTime -= time;
    }

    public void Fire() 
    {
        
        if ( cooldownTime <= 0f && mag>0) 
        {
            RayCast.GetRay();
            RayCast.GetRay(Muzzle.transform.position,Vector3.forward,0f);     

            cooldownTime = 0.1f;
            mag = mag - 1;
            if (mag == 0)
            {
                reload();
            }
        }
    }

    public void reload()
    {
        mag = magSize;
    }

    
}
}

在开始之前,我不得不感谢大家帮我解决问题

我已经看过Getting CS1513 } 预期的,但所有 的大括号都在那里但它不起作用。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;





public class rifle : Weapon
{
    // Start is called before the first frame update
    private float cooldownTime = 0f;
    private float mag = 5;
    private int magSize = 5;
    private GameObject Muzzle = GameObject.Find("RifleMuzzle"); //I tried to deleted this,not working T_T
    public void Awake() 
    {  //<-- Assets\player\playercontroller\armory\rifle.cs(13,6): error CS1513: } expected
       [inject]
       Gameobject RifleMuzzle; 
    }  //<-- What complier asked for.
       
    

    public void Cooldown( float time ) 
    {
        cooldownTime -= time;
    }

    public void Fire() 
    {
        
        if ( cooldownTime <= 0f && mag>0) 
        {
            RayCast.GetRay();
            RayCast.GetRay(Muzzle.transform.position,Vector3.forward,0f);     

            cooldownTime = 0.1f;
            mag = mag - 1;
            if (mag == 0)
            {
                reload();
            }
        }
    }

    public void reload()
    {
        mag = magSize;
    }
}  //<-- Assets\player\playercontroller\armory\rifle.cs(47,1): error CS1022: Type or namespace definition, or end-of-file expected 
//If I delete line 13,it'll disappared.
4

1 回答 1

1

[Inject]

只允许在

  • 构造函数
  • 方法
  • 范围
  • 场地
  • 财产

对于你里面的局部变量 是不合适的(实际上没有属性 is)。因此,编译器只是假设您想在引入具有该属性的类字段之前关闭该方法。RifleMuzzleAwakeAwake RifleMuzzle[Inject]


它可能应该是

[Inject]
private GameObject Muzzle; //or RifleMuzzle 

private void Awake() 
{
   // Wouldn't be needed if empty anyway
}
于 2021-04-29T16:14:12.097 回答