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
0
Question by ZeHgS · Jun 19, 2015 at 10:20 PM · objectgameaccesscannot

Can't access gameObject from the script it is attached to

Hi guys! I am new to Unity and I'm trying to attach a script to a GameObject I made. However, even though the script is attached in the Editor like it should be, when I'm trying to access gameObject in Visual Studio, which I use instead of their default one, no gameObject is shown in the auto-complete list. In the list, only the class GameObject is shown. Why is this happening?

Thanks a lot!

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
0

Answer by DoTA_KAMIKADzE · Jun 19, 2015 at 10:21 PM

Let me get it straight - if I got you right, you for some reason think that if you have game object named GO1 (for example) and you attach to it script SC1 as component then insided SC1 you can get literaly "GO1" from Intellisense?

If I got you right - then no it doesn't work like that. It doesn't depend on IDE either.

If you want to get a gameObject you're attached to, then just call:

  gameObject //note that it starts from lowercase letter unlike the class

NOTE: your script name should be equal to your class name (that inherits MonoBehaviour) inside it, in order to be properly attachable to game object and work as expected.

If I got you wrong then update your question with a better explenation of your problem.

P.S. Just for example purpose, create script named "SC1.cs" and insert this code:

 using UnityEngine;
 using System.Collections;
 
 public class SC1 : MonoBehaviour
 {
     void Start()
     {
         Debug.Log(gameObject.name);
     }
 }

attach it to your game object and see the result in console log after you press Play or run the build.

P.P.S. Though if you use JavaScript(UnityScript) then it will be pretty much blind coding with Visual Studio.

Comment
Add comment · Show 3 · 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 ZeHgS · Jun 19, 2015 at 10:41 PM 0
Share

Thank you very much for your answer! To clarify:

I have a "Ground" GameObject in Editor, and I attached a script called "Ground" of a public class called "Ground" inheriting from "$$anonymous$$onoBehaviour". What I wanted was to be able to call gameObject and access it through ".", but no list with gameObject's members appeared when I tried it.

I was going to create a box collider variable in my class which I would use to store the box collider attached to gameObject by using "gameObject.GetComponent()", but I was unable to access gameObject in the space used for declaring member variables.

By chance, I found out that if I tried calling gameObject inside one of the class functions, then auto-complete would work. Why is this? Here is a screenshot of what I mean

alt text

The same goes for the "." operator. Whenever I use it to access gameObject's members inside a function it works, but it doesn't outside.

I don't know whether this should be happening, but it is fine, however. I can just use the constructor to do what I wanted!

weird.png (50.8 kB)
avatar image DoTA_KAMIKADzE · Jun 19, 2015 at 10:50 PM 0
Share

No no no, you can't execute anything in how you call it "declaring zone", this is not how c# works, as ins$$anonymous$$d to get your collider you should do something ~like that:

 BoxCollider yourColider;
 void Awake()
 {
     yourColider = GetComponent<BoxCollider>();
 }

or if you have multiple colliders:

 BoxCollider[] yourColider;
 void Awake()
 {
     yourColider = GetComponents<BoxCollider>();
 }
avatar image ZeHgS · Jun 19, 2015 at 10:51 PM 0
Share

Oh, ok! Thanks a lot for your help

avatar image
0

Answer by engieelec · Jun 20, 2015 at 11:45 AM

Try: http://docs.unity3d.com/Manual/VisualStudioIntegration.html

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

damage game object 1 Answer

Health Bar to Render On Top 2 Answers

Any way to stack an object in a top down 2d game? 0 Answers

How do you Find Object through tags in javascript? 3 Answers

Game object position. 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