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?
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.