Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting warnings when refactoring my code

Discussion in 'Scripting' started by dancinoman, Feb 17, 2017.

  1. dancinoman

    dancinoman

    Joined:
    Jan 31, 2016
    Posts:
    11
    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?
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.    public GameObject HookThrown;
    4.    public GameObject HookOnPlayer;
    5.    private GameObject AirHook;
    6.    private GameObject HookPlayer;
    7.  
    8. private void FixedUpdate()
    9.     {
    10.        HookLaunch();
    11.      }
    12.  
    13. void HookLaunch()
    14. {
    15.      if (Input.GetMouseButton(0))
    16.        {
    17.           Vector3 PlayerLocation = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    18.           InstantiateObjects(AirHook, HookThrown. PlayerLocation) ;
    19.           InstantiateObjects(HookPlayer, HookOnPlayer, PlayerLocation) ;
    20.        }
    21. }
    22.  
    23. void InstatiateObjects(GameObject Prefab, GameObject SceneObject, Vector3 CoordinateTo)
    24. {
    25.         Prefab = Instantiate(SceneObject, CoordinateTo, Quadernion.identity) as GameObject;
    26.        //other repeating statements
    27. }
     
    Last edited: Feb 17, 2017
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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

    Code (csharp):
    1.  
    2. AirHook = InstantiateObjects(HookThrown, PlayerLocation);
    3.  
    4. GameObject InstantiateObjects(GameObject sceneObject, Vector3 coord)
    5. {
    6.     var prefab = Instantiate(.....);
    7.  
    8.     return prefab;
    9. }
    10.  
     
    dancinoman and Suddoha like this.
  3. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    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.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
    KelsoMRK likes this.
  5. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    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.

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.    public GameObject HookThrown;
    4.    public GameObject HookOnPlayer;
    5.    private GameObject AirHook = null;
    6.    private GameObject HookPlayer;
    7. private void FixedUpdate()
    8.     {
    9.        HookLaunch();
    10.      }
    11. void HookLaunch()
    12. //etc
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This would get rid of the warning but, as @Suddoha mentioned, OP's approach won't do what he thinks it will.
     
  7. dancinoman

    dancinoman

    Joined:
    Jan 31, 2016
    Posts:
    11
    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
     
  8. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    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.

    Code (csharp):
    1.  
    2. InstantiateObjects(ref AirHook, HookThrown. PlayerLocation) ;
    3. void InstatiateObjects(ref GameObject Prefab, GameObject SceneObject, Vector3 CoordinateTo)
    4.  
    This says that now any changes/assignments done to "Prefab" are actually done to "AirHook".
     
    Last edited: Feb 18, 2017