unity-game-enginezenject

Unity 19.4.15 Zenject 9.8 CS1513: But Already Closed All Functions/Class


[edited]

Move [inject] label isn't working //<--Because is [Inject]

Now it has two error:

Assets\player\playercontroller\armory\rifle.cs(10,10): error CS0246: The type or namespace name 'injectAttribute' could not be found (are you missing a using directive or an assembly reference?)

Assets\player\playercontroller\armory\rifle.cs(10,10): error CS0246: The type or namespace name 'inject' could not be found (are you missing a using directive or an assembly reference?)

New code is here.

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;
    }

    
}
}

Before started,I had to thanks Everyone help me to figure out problem

I've already looked Getting CS1513 } Expected, but all braces are there But it's not working.

Here's my code:

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.

Solution

  • [Inject]

    Is only allowed on

    For your local variable RifleMuzzle inside of Awake it is not suitable (actually no attribute is). Therefore the compiler simply assumes you wanted to close the Awake method before introducing a class field RifleMuzzle with the [Inject] attribute.


    It should probably rather be

    [Inject]
    private GameObject Muzzle; //or RifleMuzzle 
    
    private void Awake() 
    {
       // Wouldn't be needed if empty anyway
    }