Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DoctorSauce · Oct 15, 2013 at 04:55 PM · c#error

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by PTerto · Oct 15, 2013 at 05:00 PM

try this :

Tranform target; GameObject Player;

Player = GameObject.FindWithTag ("Player"); target = Player.transform;

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoctorSauce · Oct 15, 2013 at 05:03 PM 0
Share

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?

avatar image PTerto · Oct 15, 2013 at 05:04 PM 0
Share

Sorry, I edit, try again xD

avatar image DoctorSauce · Oct 15, 2013 at 05:10 PM 0
Share

Haha brilliant, it works perfectly. Thanks again!

avatar image gokwangwoo · Mar 17, 2017 at 07:29 AM 0
Share

Thanks for your suggestion

avatar image
6

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Oct 15, 2013 at 05:17 PM 1
Share

I find this to be a much more appropriate answer over the accepted one. +1

avatar image DoctorSauce · Oct 15, 2013 at 05:33 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges