Search Unity

Question How to Change a Configurable Joint in C#

Discussion in 'Scripting' started by Bustercody5, Sep 27, 2020.

  1. Bustercody5

    Bustercody5

    Joined:
    Aug 22, 2020
    Posts:
    10
    Hello!
    I have been trying to a game that when my character is hit the Configurable joint on it that holds it up, stops applying force and effectively creates a ragdoll. When I run this it say fix all compiler errors before running. I get no other error messages.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.     var CJ = LBleg.gameObject.GetComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.         LBleg = GameObject.Find("/Enemy/LBleg");
    14.     }
    15.     void OnTriggerEnter(Collider collider)
    16.     {
    17.  
    18.         if (collider.tag != "ground")
    19.         {
    20.             CJ.angularXDrive.positionSpring = 0;
    21.         }
    22.        
    23.     }
    24.  
    25. }
    26.  
     

    Attached Files:

  2. Bustercody5

    Bustercody5

    Joined:
    Aug 22, 2020
    Posts:
    10
    Thank you for any help!
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,910
    It would help if you shared the compile errors you are getting (in full).
     
  4. Bustercody5

    Bustercody5

    Joined:
    Aug 22, 2020
    Posts:
    10
    I will include a video of the errors, and another thing I would like to mention is that I have also tried this code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Enemy : MonoBehaviour
    6. {
    7. private ConfigurableJoint joint;
    8.     void Start()
    9.     {
    10.         joint = gameObject.GetComponent<ConfigurableJoint>();
    11.         LBleg = GameObject.Find("/Enemy/LBleg");
    12.     }
    13.     void OnTriggerEnter(Collider collider)
    14.     {
    15.         if (collider.tag != "ground")
    16.         {
    17.             LBleg.joint.angularXDrive.positionSpring = 0;
    18.         }
    19.      
    20.     }
    21. }
     
  5. Bustercody5

    Bustercody5

    Joined:
    Aug 22, 2020
    Posts:
    10
    here
     

    Attached Files:

  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,910
    This line is your problem for a few reasons:
    Code (CSharp):
    1. var CJ = LBleg.gameObject.GetComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
    1. You never declared the LBleg variable. You need to declare your variables in C#. For example
    Code (CSharp):
    1. public GameObject LBleg;
    2. You cannot have code like this floating around outside of a function. Put this code inside Start().
     
    Last edited: Sep 28, 2020
  7. Bustercody5

    Bustercody5

    Joined:
    Aug 22, 2020
    Posts:
    10
    @PraetorBlue is this correct?
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.  
    9.     public GameObject LBleg;
    10.     void Start()
    11.     {
    12.  
    13.         var CJ = LBleg.gameObject.GetComponent(typeof(ConfigurableJoint)) as ConfigurableJoint;
    14.     }
    15.     void OnTriggerEnter(Collider collider)
    16.     {
    17.  
    18.  
    19.         if (collider.tag != "ground")
    20.         {
    21.             CJ.yDrive.positionSpring = 15;
    22.         }
    23.        
    24.  
    25.  
    26.  
    27.  
    28.     }
    29.  
    30. }
    31.  
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,910
    Not quite. You can't declare a variable on one function and use it in another. You're doing so with your CJ variable. Try this:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.     public GameObject LBleg;
    9.     ConfigurableJoint CJ;
    10.  
    11.     void Start()
    12.     {
    13.         CJ = LBleg.gameObject.GetComponent<ConfigurableJoint>();
    14.     }
    15.  
    16.     void OnTriggerEnter(Collider collider)
    17.     {
    18.         if (collider.tag != "ground")
    19.         {
    20.             CJ.yDrive.positionSpring = 15;
    21.         }
    22.     }
    23. }
    24.