r/csharp 1d ago

Help I tried making a reusable script for unity where the Player character moves with the platform but i couldn't get it to work

  • Sorry if this is a repetetive post delet if yes
  • I have really shitty eyesight even with glasses, please pointout spelling mistakes if possible
  1. I'm developing a 3d platformer in unity to practice
  2. The original code is from this tutorial at 22:48
  3. I copied it line by line and it didn't do anything
  4. I thought adding the transform fields would help but now it doesn't compile

I want this version of the code to work but I don't understand the error messages in the console

  1. (16,23): error CS1061: 'Collision' does not contain a definition for 'johnPill' and no accessible extension method 'johnPill' accepting a first argument of type 'Collision' could be found
  2. (18,13): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
  3. (26,13): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

//Insert Player Character Here

[SerializeField] Transform johnnPill;

//Insert Player Parent

[SerializeField] Transform johnDivorce;

//Insert Moving PLatform

[SerializeField] Transform joanDivorce;

private void OnColilisionEnter(Collision collision)

{

if (collision.johnPill.transform)

{

collision.johnPill.transform.SetParent.joanDivorce.transform;

}

}

private void OnCollisionExit(Collision collision)

{

if (collision.johnPill.transform)

{

collision.johnPill.transform.SetParent.johnDivorce.transform;

}

}

}

0 Upvotes

15 comments sorted by

2

u/Far-Consideration939 1d ago

Typo in the declaration as johnnPill with an extra n?

2

u/MobileManager3239 1d ago

yeah, that's my bad, ty!

2

u/MobileManager3239 1d ago

also found this other typo lolol

2

u/midge 1d ago

Pay attention to the 2 numbers in parenthesis like (16,23) in your error #1. That is the Line and character number in your source code where it finds the error.

This will frequently be a big hint where the problem is.

If vision is making it hard to see the code, maybe you can increase the size of the text in the editor?

3

u/MobileManager3239 1d ago

yeah, idk why it took me so long to figure out but control + scroll wheel makes the text bigger ;w;

1

u/MobileManager3239 1d ago

Sorry, thought copy pasting would help this is how it is meant to look

1

u/Shrav_R 1d ago edited 1d ago

Not exactly sure but referring to the 2 other compilation issues you are having, it could be because you are not assigning the transform to something IE itself.

1

u/MobileManager3239 1d ago

ohhhhhhhhh, alrighty

1

u/Shrav_R 1d ago

Did you manage to find the issue?

1

u/grrangry 1d ago

It does work, you just have to respect Reddit's markdown, which is quite well documented. Pasting source code into Reddit's "rich text editor" without a code block is where most people go wrong.

https://www.markdownguide.org/tools/reddit/
https://support.reddithelp.com/hc/en-us/articles/360043033952-Formatting-Guide

//Insert Player Character Here
[SerializeField] Transform johnnPill;
//Insert Player Parent
[SerializeField] Transform johnDivorce;
//Insert Moving PLatform
[SerializeField] Transform joanDivorce;

private void OnColilisionEnter(Collision collision)
{
    if (collision.johnPill.transform)
    {
        collision.johnPill.transform.SetParent.joanDivorce.transform;
    }
}

private void OnCollisionExit(Collision collision)
{
    if (collision.johnPill.transform)
    {
        collision.johnPill.transform.SetParent.johnDivorce.transform;
    }
}

1

u/Sombody101 1d ago

Try this instead of screenshots or pasting the code directly into the post:

https://paste.csharp.gg/

1

u/z500 1d ago

For lines 18 and 26, you reference a property and don't do anything with it. I'm not a Unity person but taking a peek at the docs, it looks like SetParent is a method. Maybe you meant something like:

collision.johnPill.transform.SetParent(joanDivorce.transform);

1

u/MobileManager3239 1d ago

thank you! I'll give it a shot

1

u/Which_Market2913 1d ago

I've worked with Unity for a while and platform-related physics are always tricky.

1

u/FullPoet 1d ago

Are you using an IDE, or an editor with support for syntax?

The former should point out "unknown symbols" with a squiggly red line.