Well, I’m making a 2D platform game and getting an error:
Assets\Pixel Adventure 1\Assets\Scripts\Frog.cs(51,64): error CS1061: ‘Transform’ does not contain a definition for ‘y’ and no accessible extension method ‘y’ accepting a first argument of type ‘Transform’ could be found (are you missing a using directive or an assembly reference?)
I’ll let the frog code fixed, so you guys can help me to fix it,
I appreciate any help!
And somehow I can’t make that frog (the frog is the enemy of the game) turns his face when he goes to another direction, you know?
So, when he goes to the right sounds like he’s dancing a moonwalker lol
Hope you can help me,
Thank you so much,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Frog : MonoBehaviour
{
private Rigidbody2D rig;
private Animator anim;
public float speed;
public float moveTime;
public Transform headPoint;
private bool dirRight = true;
private float timer;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (dirRight)
{
//if It's true, the saw goes to the right
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
else
{
//if it isn't true, it goes to the left
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
timer += Time.deltaTime;
if (timer >= moveTime)
{
dirRight = !dirRight;
timer = 0f;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "Player")
{
float height = col.contacts[0].point.y - headPoint.y;
}
}
}
First, it’s important to understand what your error message means. Let’s take a look at it.
That makes sense, right? If we check out the API documentation page for Transform, we can look under Properties and see that it does, in fact, not have a property “y” that we can access. So let’s look at the line where the error is occurring (line 51).
You’re referencing the y property twice on this line, so it might not be immediately obvious which one is causing the error. If you were doing this on your own, it might be helpful to break this line down into two lines, each containing only one of the parts where you’re accessing y to see where the problem is. If we check out the page for Collision2D, we’ll see it does have a contacts property, which is an array of ContactPoint2D, which does have a point property, which is a Vector2, which does have a y property. So this part is probably not the issue.
But if you look at the other part, you’ll see that your variable headPoint is a Transform. Transform does not have a property called y. What you probably want is the position property of that Transform, which does have a y property for you to access. When you think about that, it makes sense, since Transforms are collections containing position, rotation, and scale values.
I don’t really know what you’re trying to say here. Might be worth making that a separate post, but you’ll have to be a lot more specific and descriptive about your issue, including some screenshots of what’s going on, maybe.
I understand your frustration and confusion, but I think it’s worth asking… Do you just want to get this script working, or do you want to learn how to be a game developer?
Being able to understand error messages and use them to fix issues in your code is a huge part of being a developer. Being able to read through documentation and understand it is, too. I’m honestly not sure how to say much more beyond what I provided in my first response.
Did you write this code yourself or copy it from somewhere? Was it part of a tutorial you’re following? When copying/following along with tutorials, it’s important to pay close attention and do things exactly as instructed. Programming is a pretty precise skill where things like capitalization, punctuation, and missing words really matter.
The environment you use can help also. I am on Windows and use Visual Studio for C# editing. It has “Intellisense” and would not have let me enter the line without indicating it was in error. Actually, while typing it would offer me the available choices for an object when I typed the “.” and “y” would not have been one at that point.
shout out to @Kurt-Dekker and @Schneider21 - I have been on a lot of different programming forums over the years and I don’t think I have ever seen another one where nearly all responses are so polite and thorough. I think about a week after Compuserve went live, online flaming was invented.