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. Dismiss Notice

ParentConstraint usage

Discussion in 'Scripting' started by Bannanaking3, Jun 23, 2022.

  1. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    112
    (Fair warning, I'm still very nooby when it comes to c# and unity so there's a good chance I'm using the wrong terms for things here, please bear with me) I'm trying to create a flying enemy in a 2D platformer that will get grabbed by the player on collision. The enemy will also slow the player's fall allowing them to span bigger gaps. However, I have no I idea how to activate the patent constraint in a script. I think this is because I don't know how to declare an object to use it with. How do I declare an object in a way that lets me use parent constraint?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Unity has pretty decent documentation on a lot of their stuff, did you try viewing the page on ParentConstraint? It tells you all the properties and methods you can access through code, gives a description of what each does, and sometimes even has examples of how to use them.
     
    Bunny83 likes this.
  3. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    112
    I did, the issue is I'm not sure how to reference the ParentConstraint from the object.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Can you not just use GetComponent on the object, like you would for any other component?
     
    Bunny83 likes this.
  5. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    112
    I just get an error message saying GameObject does not contain a definition for ParentConstraint.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Each GameObject does not inherently know what components it has, so you have to either use the method GetComponent to check for one, or to assign it in the Inspector. You cannot just try to access it like a property, which is what I'm guessing you're doing.

    Code (CSharp):
    1. ParentConstraint pc = GetComponent<ParentConstraint>(); // <-- works
    2.  
    3. ParentConstraint pc = gameObject.ParentConstraint; // <-- bad, won't work
     
  7. hobo37

    hobo37

    Joined:
    Feb 7, 2020
    Posts:
    1
    do you by any chance know what to do if this doesnt work

    unity insists that ParentConstraint doesnt exist, like in: the type doesnt exist not the variable
     
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Did you make sure to include the libaries ParentConstraint is from? If you're using Visual Studio or some other good IDE it should auto-fill it in for you.

    I would also recommend not necro-ing an old post, your problem isn't the same as the one above. Instead, you can just create a new thread. When doing so, I'd recommend making sure your post is as detailed as possible, as you have not given a lot of information. A sample of your code would be a good starting place. If you do decide to share code, please make sure to use code tags, which you can find how to use on the top sticky thread of this forum.
     
    Kurt-Dekker likes this.