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

No object-instance by using GetComponentInParent

Discussion in 'Scripting' started by Christoo25, Jun 18, 2017.

  1. Christoo25

    Christoo25

    Joined:
    Jun 15, 2017
    Posts:
    7
    Hello everyone,

    i am christopher and I come from germany.
    So i apologize in advance for my bad english^^

    I have get the asset "Ultimate Survival" and so on, I have see some tutorials about this asset
    and integrate something from this tutorials into my project.

    So on, so good. The tutorials are from the "messy Coder" - guy which has fantastic detailed
    examples.

    But now, I implement this example in this video:


    I have everything done in this video but I get a nullpointer.

    When I use this code, the method GetComponentInParent return null
    in the class "AmmoDragHandler" in row 32.
    Code (CSharp):
    1.  
    2. itemDragHandler = transform.GetComponentInParent<ItemDragHandler>();
    3.  
    And I get a NullPointer in the class "ItemDragHandler" at row number 147
    Code (CSharp):
    1.  
    2. if (ammoDragHandler.LoadAmmo(initialSlot, itemUnderPointer, m_DraggedItem))  {
    3. return;
    4. }
    5.  
    What can I do to solve this problem?`

    kind regads,

    Christopher T.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For the first one, there is no ItemDragHandler found.. you have to make sure it's there on the object or parent.
    Same thing with the next one, in that if it's null, it wasn't found/assigned properly. Can't tell from the code you provided, but if you fetched the reference to ammoDragHandler at some point earlier, it may have come back null. If it's possible that it can come back null, you have to guard against calling it when it does. If it should never be possible to come back as such, then you should make sure that it's added properly to any game object, as well as referenced correctly.
     
    Braineeee likes this.
  3. Christoo25

    Christoo25

    Joined:
    Jun 15, 2017
    Posts:
    7
    Hi,

    I have seen that I have one Option which is set differently, when I select a weapon in the Inspector, as the guy in the video has.
    There is an option called "ammoDragHandler", which is not set, but in the Video at 33:00
    You can see that this is set with "InventoryController".

    But when I try to set the InventoryController, unity say that it can't add this.

    What could it be? I checked the class extension of the class AmmoDragHandler and the in the ItemDragHandler
    which are MonoBehavier<ItemDragHandler>.

    So I think that was right, cause the guy in the video has it too.
     
  4. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Someone else and I ran into problems when (I think) assigning transforms / GameObjects in the inspector. Check this tread out for more info.

    Jemand anderes und ich lief in Probleme, wenn (ich glaube) Zuweisungen Transformationen / Game Objects im Inspektor. Überprüfen Sie diese Lauffläche für weitere Informationen.

    https://forum.unity3d.com/threads/parent-object-is-null.477461/
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you get this solved?
     
  6. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    The code that I posted in the other link above worked to get me around the problem. But hopefully unity will provide a fix for it soon.
     
  7. Christoo25

    Christoo25

    Joined:
    Jun 15, 2017
    Posts:
    7
    I solved it yes =)
    But I think this was a little bit stupid issue for myself.

    I had to switch the AmmoDragHandler skript into the Inspector while the InventoryController is selected.
    I didn't see it in the video, but so on, the skript could reference the ammoDragHandler-object from the function.

    I Hope my answer are readable :D
    If you don't understand what I try to tell you, I can write it in a different formulation.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm glad that you got it fixed :)
     
  9. Christoo25

    Christoo25

    Joined:
    Jun 15, 2017
    Posts:
    7
    Thank's =)

    I know this is not the right place to write it down but,
    may you know some tutorial to import a new NPC to the game?

    I did the tutorial from the guy at the video above, but in this video
    he show, how can I add an NPC wich is likely the same as one other NPC which already exists.
    The only difference between each other is, that the new NPC has another Model :D

    For example I want to add this Monster to the game https://www.assetstore.unity3d.com/en/#!/content/48933

    I tried it like the NPC in the video, but I can't add a ragdoll over the gui-wizard from Ultimate-Survival-Tool
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe make a new thread describing what you're looking for, for some good feedback (since it's an entirely new subject). :)