0
\$\begingroup\$

i have a problem that i don't understand why is happenning and it's driving me mad. I'm trying to code a script to put the player in the car by disabling the third person controller and then Transform.SetParent. Here's the code:

  public ThirdPersonCharacterControl Controller;
  public GameObject Player;
  public GameObject Car;

 public void PutWheel()
    {
        if(isInteractive)
        {
            if(isCar)
            {
                PlacePlaceables();
                Controller.enabled = false;
                Player.transform.SetParent(Car);
            }
        }
    }

The error is:

Assets/Scripts/Player/Interactible.cs(361,34): error CS1502: The best overloaded method match for `UnityEngine.Transform.SetParent(UnityEngine.Transform)' has some invalid arguments

Assets/Scripts/Player/Interactible.cs(361,44): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `UnityEngine.Transform'

Why is that happening? It doesn't make sense to me because everything seems correct.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Car is a GameObject

SetParent wants to act on a Transform

So, two options:

  1. Change public GameObject Car to public Transform Car to store the car as a reference to its Transform all along, or...

  2. Use Player.transform.SetParent(Car.transform) to access the Transform component attached to the car.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is exactly what the compiler errors are telling you, so in future, remember to look carefully at the types described in the error and the types that you're using to be sure you're providing the right argument types. \$\endgroup\$
    – DMGregory
    Commented Jun 3, 2019 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .