• 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 "MonoBehaviour". 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 instead 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't destroy a script. 1 Answer

How to make objects appear randomly around a spot 2 Answers

How could I get a selection set of GameObject sharing a material or a script in Editor? 1 Answer

Trying to access destroyed object 1 Answer

get all scripts attached to gameobject 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges