Getting warnings when refactoring my code

The Script below works very fine. But I get this warning “CS0649 C# Field PlayerController.AirHook is never assigned to, and will always have its default value null”.

Its odd because its obviously false.
This warning appears for every Objects refering to the local function. How can I get rid off these warnings and still get nicer and clean code?

public class PlayerController : MonoBehaviour
{
   public GameObject HookThrown;
   public GameObject HookOnPlayer;
   private GameObject AirHook;
   private GameObject HookPlayer;

private void FixedUpdate()
    {
       HookLaunch();
     }

void HookLaunch()
{
     if (Input.GetMouseButton(0))
       {
          Vector3 PlayerLocation = new Vector3(transform.position.x, transform.position.y, transform.position.z);
          InstantiateObjects(AirHook, HookThrown. PlayerLocation) ;
          InstantiateObjects(HookPlayer, HookOnPlayer, PlayerLocation) ;
       }
}

void InstatiateObjects(GameObject Prefab, GameObject SceneObject, Vector3 CoordinateTo)
{
        Prefab = Instantiate(SceneObject, CoordinateTo, Quadernion.identity) as GameObject;
       //other repeating statements
}

That’s a weird pattern (even I got hung up on it at first). The more standard solution would be to have InstantiateObjects return a GameObject and assign AirHook to the result of the method call

AirHook = InstantiateObjects(HookThrown, PlayerLocation);

GameObject InstantiateObjects(GameObject sceneObject, Vector3 coord)
{
    var prefab = Instantiate(.....);

    return prefab;
}
2 Likes

It’s because the instantiation is inside of an if condition. Just a warning that if the if condition is never met, then it will always be null.

It’s not a false warning.
It’s totally correct.

Whichever object is reference by AirHook can be accessed via the parameter “Prefab” as soon as you pass it to the method, as it’s just another reference to the actual object.
However, an assignment to the “Prefab” variable will not affect the “AirHooK” variable so from the compiler’s point of view nothing is ever going to be assigned to “AirHook” (because it does not take Unity’s serialization inito account).

Other than that, do not query Input in FixedUpdate directly. Use a flag in Update if you need to evaluate Input in FixedUpdate, or move the code to either Update or LateUpdate.

This being said, the warning is totally justified.

Nope. Even if there wasn’t an if and the code would always run, the warning should come up.

1 Like

Have you tried just assigning AirHook to null?
It’s not complaining that AirHook is null. It’s complaining it’s never assigned to. Assigning it to null will address the compilers concerns.

public class PlayerController : MonoBehaviour
{
   public GameObject HookThrown;
   public GameObject HookOnPlayer;
   private GameObject AirHook = null;
   private GameObject HookPlayer;
private void FixedUpdate()
    {
       HookLaunch();
     }
void HookLaunch()
//etc

This would get rid of the warning but, as @Suddoha mentioned, OP’s approach won’t do what he thinks it will.

I’ve done it for many cases that I had in my other script and it worked! What I understand is that it is setting the GameObject to null at Start until something change it. Amazing!

I’ve modified my script with the instantiate parameters. And what you say is right the return the values fixes the problem. This was all about saving lines. Now it works

You probably don’t understand how references work in C# yet.

There is a keyword you can use called “ref”.
https://msdn.microsoft.com/en-us/library/14akc2c7.aspx
If you wanted to assign back to AirHook, you needed to use the “ref” keyword.

InstantiateObjects(ref AirHook, HookThrown. PlayerLocation) ;
void InstatiateObjects(ref GameObject Prefab, GameObject SceneObject, Vector3 CoordinateTo)

This says that now any changes/assignments done to “Prefab” are actually done to “AirHook”.