Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I can't set a parent with an npc

Discussion in 'Scripting' started by hudsondbr, Jan 17, 2022.

  1. hudsondbr

    hudsondbr

    Joined:
    Aug 7, 2021
    Posts:
    2
    please help me I have this code for 3 days and I can't make transform.parente work, I'm trying to put an object inside an npc with transform.parent, but it doesn't work

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class npc : MonoBehaviour
    7. {
    8.     NavMeshAgent inte;
    9.     public Transform bolan;
    10.     public float distanciaDaBolan;
    11.     public bool comBolan;
    12.     Rigidbody fisicaDaBolan;
    13.     public float forçaDoChuta;
    14.     public Transform goalPos;
    15.     public Transform eu;
    16.     public static bool taComBola;
    17.  
    18.     void Start()
    19.     {
    20.         distanciaDaBolan = Vector3.Distance(transform.position, bolan.position);
    21.         inte = GetComponent<NavMeshAgent>();
    22.         fisicaDaBolan = bolan.GetComponent<Rigidbody>();
    23.     }
    24.  
    25.    
    26.     void Update()
    27.     {
    28.         if(distanciaDaBolan < 1.128372 && comBolan == false && npc.taComBola == false){
    29.             comBolan = true;
    30.             bolan.SetParent(transform);
    31.             bolan.localPosition = new Vector3(0f, -0.8f, 0.75f);
    32.         }
    33.  
    34.         if(comBolan == true){
    35.             bolan.SetParent(transform);
    36.             fisicaDaBolan.isKinematic = true;
    37.         }else if(comBolan == false){
    38.             inte.destination = bolan.position;
    39.             fisicaDaBolan.isKinematic = false;
    40.         }
    41.     }
    42.  
    43.     private void OnCollisionEnter(Collision other) {
    44.         if(other.gameObject.tag == "ball"){
    45.             bolan.SetParent(transform);
    46.         }
    47.     }
    48. }
    this is the code i am currently using
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,518
    We know that SetParent didn't stop working, so perhaps:

    - your code isn't running
    - you are parenting the wrong thing
    - you are parenting the thing to something incorrect.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    hudsondbr likes this.
  3. hudsondbr

    hudsondbr

    Joined:
    Aug 7, 2021
    Posts:
    2
    that's right, thanks a lot man, the problem was that the player script was always setting the parent to null
    thanks again
     
    Kurt-Dekker likes this.