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

problem with object reference

Discussion in 'Scripting' started by Trild123787898, Feb 17, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    How can I insert a reference in the inspector on an object in a script? + which has a rigid body component.
    Initially, there is already a hook on the same object, in some circumstances I need to remove the rigid body from the object, and this is very important, naturally the power is also reset, and when I add the rigid body again, the power will not automatically take its place, actually I need to add it again but something doesn’t work for me

    place for a snare in another script (mehanic)

    Code (CSharp):
    1. public Rigidbody det5;
    2.  
    here is the main

    Code (CSharp):
    1. private GameObject det1;
    2. private GameObject rb;
    3. Rigidbody rb_det1;
    4. mehanic rigid;
    5. void Start()
    6. {
    7.    
    8.     rigid = GetComponent<mehanic>();
    9. }
    10. void OnTriggerEnter(Collider enter)
    11. {
    12.     if (enter.tag == "terrain")
    13.     {
    14.        
    15.         det1 = GameObject.FindWithTag("detail");
    16.         Destroy(det1.GetComponent<Rigidbody>());
    17.     }
    18. }
    19. void OnTriggerExit(Collider exit)
    20. {
    21.     if (exit.tag == "terrain")
    22.     {
    23.        
    24.         rb_det1 = det1.AddComponent<Rigidbody>();
    25.         rb_det1.mass = 10;
    26.         rb_det1.drag = 1;
    27.         rigid.det5 = rb_det1;
    28.         //det1.AddComponent<Rigidbody>();
    29.     }
    30. }
    31.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you be more clear with what you're asking?
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    in the inspector, I drag the desired object in det5, then I delete this object and add it again, and the place is reset to zero, how can I again drag the desired object in the det5 field, but in the script
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    But it looks like you're already doing that on line 27. Are you getting an error or something?

    If you're going to add/remove your rigidbody component like this, it is probably a lot simpler to just disable and reenable it by the way.
     
  5. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    2 line 27 I thought what I was doing right, can you explain how to implement it better, or where to look?
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Basically you can't. From the scripting point of view the new object created is completely unrelated to deleted one so there's no way to refill that reference field to the "same" object because it is the same only inside your head. But if there's some fixed criteria to find that object exists then it is possible to do in OnValidate, for instance.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The rigidbody you're adding is a different rigidbody, so the reference won't "update" when it's replaced.

    The most straight-forward thing would be to reference the GameObject the rigidbody is on instead, and then fetch the reference when you need it:

    Code (csharp):
    1. // instead of:
    2. public Rigidbody det5;
    3.  
    4. // you do:
    5. public GameObject det5Container;
    6.  
    7. private Rigidbody _det5;
    8. public Rigidbody Det5 {
    9.     get {
    10.         if (_det5 == null)
    11.             _det5 = det5Container.GetComponent<Rigibdoy>();
    12.         return _det5;
    13.     }
    14. }
    An alternative is to have the class that's destroying and adding these rigidbodies know about the class that has the field, and then update it when the new one is made, but that increases your coupling a bit, so it's up to you what you think is best.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Also like don't name your fields "det5" if you want to remember what the hell they do when you read the code again in two weeks, but that's a different conversation :p
     
  9. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    thanks for the help, but it seems the matter is even more complicated, I forgot to mention that I deactivate the rotation of physics with this det5 object, with the help of buttons and it actually stands still, even if I specify _det5
     
  10. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    if I copy a component and then paste it, it won’t be considered another?
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Of course it will. The copy is not the same as original.
     
  12. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    What do I need to do in this situation?
     
  13. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Either set references manually, or via script if you can formalize search logic.
     
  14. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I'm trying to do it, but I can’t do it
     
  15. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Simple example of formalized search logic:

    Code (CSharp):
    1. myRb = FindGameObjectWithTag("PlayerBody").GetComponent<Rigidbody>();
    Another example:

    Code (CSharp):
    1. myCanvasGroup = GetComponentInParent<CanvasGroup>();
    Put those into OnValidate callback and all your references always will be valid.
     
  16. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I tried to do this, also in that script I delete the rigidbody, added a variable to check tes2 but it does not work

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (sw.tes2 == true)
    4.         {
    5.             myrb = GameObject.FindGameObjectWithTag("detail").GetComponent<Rigidbody>();
    6.             myrb = det5;
    7.         }
    8.     }
    9.  
     
  17. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I don't know how you declared and using myrb and det5 variables, but may be it should be

    Code (CSharp):
    1. det5=myrb;
    and not

    Code (CSharp):
    1. myrb=det5;
    ? Because this code finding a rigidbody, store a reference to it in the myrb variable, and then override that reference with whatever you have in det5 variable.
     
  18. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry but still not changed