- Home /
How do I fix UnityEngine.Transform' does not contain a definition for `GameObject'?
I've looked all over for how to fix this error and every time I think I find a fix it creates a bunch more errors. So I figured I would just ask about it. Here is the script
using UnityEngine;
using System.Collections;
public class GameCamera : MonoBehaviour {
private Transform target;
public float trackSpeed = 25;
public void SetTarget() {
target = Transform.GameObject.FindWithTag ("Player");
}
void LateUpdate() {
if (target) {
float x = IncrementTowards(transform.position.x, target.position.x, trackSpeed);
float y = IncrementTowards(transform.position.y, target.position.y+2, trackSpeed);
transform.position = new Vector3(x, y, transform.position.z);
}
}
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n);
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target;
}
}
}
I need it to find the object with the Player tag so I can switch players and the camera will start following the new player. The error is:
'UnityEngine.Transform' does not contain a definition for 'GameObject'
and it is pointing to this line:
target = Transform.GameObject.FindWithTag ("Player");
Any ideas on how I can fix this? I feel like I've tried everything, but it's probably something simple. Thanks in advance for any help!
Answer by PTerto · Oct 15, 2013 at 05:00 PM
try this :
Tranform target; GameObject Player;
Player = GameObject.FindWithTag ("Player"); target = Player.transform;
Thanks for the suggestion. I tried this though and it told me I can't convert GameObject to Transform. $$anonymous$$aybe is there a way I can change the script to make it follow a GameObject ins$$anonymous$$d of a transform?
Answer by Hoeloe · Oct 15, 2013 at 05:07 PM
It's because in most programming languages, case matters. GameObject
is not the same thing as gameObject
. GameObject
is a class (kind of like the blueprint that defines all game objects), while gameObject
is a variable, of type GameObject
, which is a member of the Transform
class (note that the same holds true of Transform
, in that it is not the same thing as transform
). Regardless, you don't actually need that, as FindWithTag
is a static method, meaning it can be called directly from the class GameObject
(i.e. it doesn't require a specific object). GameObject
, as a class, is not contained within Transform
, but is a separate entity. It just so happens that Transform
objects contain a GameObject
element, but the definition of GameObject
is independent. Thus, you don't need to look inside Transform
to find it (which is what you're doing when you write Transform.GameObject
)
I find this to be a much more appropriate answer over the accepted one. +1
This is also a great answer. I chose the first one 1) because it immediately solved my problem and 2) because it was the first one up, so it was the first one I looked at. I hope you aren't offended if I didn't pick your answer, but ima still +1 it.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity + XCode Problem (really wierd) 0 Answers
Need help calling a script to another keep getting errors 1 Answer
Dying Script error 2 Answers
WHERE´S THE ERROR?PLEASEEE 1 Answer