- Home /
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!
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.
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
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!
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>();
}
Your answer
Follow this Question
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