Is there a good guide on the basic of Unity? I started using this today, and I think it's far less intuitive than it could be.

1    06 Apr 2018 06:10 by u/AnotherNewAlt

Do the GameObjects not simply get Scripts added to them that essentially are members of the what-I-thought-was the parent component? I cannot seem to access these members in any consistent manner. Further, it's not obvious what the names are of them to even begin getting and setting things on them.

Where are the general names of the objects, and how do I access specific objects by something like an ID? All I'm seeing is object fields being called IDs, and no way to access unique objects as a whole.

i can do these things faster in a java swing program than I can here... honestly, is Unity just a crappy GUI maker or what? This seems MORE complicated than making things from scratch, at least when getting started that is.

8 comments

0

if you have a player script on an object (the player, presumably) and you want to access it from another script, you would declare a variable in the other script like:

c#:

private Player player;

and in Start() (or Awake(), or OnEnable(), or OnTriggerEnter(), or in another method/function, etc) you would add:

player = FindObjectOfType(typeof(Player)) as Player;

and then you would use the player variable to access that script.

those were just examples, but you shouldn't use FindObjectOfType in methods/functions directly. you should store a reference to the Player object, or whatever object, in Start() or Awake() and then store a reference to it in a variable in your method/function and call the necessary part of the script from there.

0

Ok, as I was typing up the problem I was having the next run I did solved it.

However there's still something important I need to know for general knowledge

player = FindObjectOfType(typeof(Player)) as Player;

How do I know the data type of the object I'm trying to access? I need the exact name. I see things in the object, that I made from the menu that indicated I was making a sprite object, the allude to it being a sprite like "sprite renderer", but if I'm trying to use the line of code you have above how do I knew the exact name to write in there so I can find what I'm looking for? I would think it would be at the top of the Inspector, but I don't think that's right because I can type anything I want in there, including spaces. And after that I see nothing consistent between the objects that actually says the name of the object in it.

0

the SpriteRenderer's type is SpriteRenderer so you would access it like:

private SpriteRenderer spriteRenderer;

and define it as:

spriteRenderer = GetComponent(typeof(SpriteRenderer)) as SpriteRenderer;

also anything that has a script on it has a type of NameOfScript.

if you want to access a script called VoatGoat you would access it like:

GetComponent(typeof(VoatGoat)) as VoatGoat;

use GetComponent when the script you want is on the same object as the script you're calling it from.

if you want the object the VoatGoat script is on you would do:

private GameObject voatGoat;

and define it as:

voatGoat = FindObjectOfType(VoatGoat)).gameObject as GameObject;

0

Ok so sprite's are called SpriteRenderer s? How can I know for sure when other objects deviate slightly from their menu name? Is this info not in the Inspector? I thought of simply viewing the script for the Object, but i coudln't find an easy way to do that for a script I didn't create (as those are stored in my Assets).

also anything that has a script on it has a type of NameOfScript.

This is part of the confusion I described above. I have a canvas component and there appear to be 4 object (scripts) that come with it: Rect Transform, Canvas, Canvas Scaler, Graphic Raycaster. If these are the names of the scripts how can I access them since there are spaces? Is there some convention where it defaults to the camel case of those script names? I remember someone mentioning this in a previous tutorial, but he just took out the space and didn't talk about space usage.

What does the name at the top of the Inspector do? Is that a unique ID for the script? As in something I might want to assign a GUID to for search simplicity? If not, is there a place I can assign something like that?

0

the premade objects you add use their type as the name, so Canvas's type is Canvas. if you want to access a component on the canvas you would reference the canvas and then get the component using canvas.GetComponent.

as far as I can remember, the spaces will not be in the name of the type. I think it automatically removes them if you use them in the name of the script, but I can't be sure as I don't use them in the names of my scripts. I use PascalCase for my scripts and methods/functions, and camelCase for my variables.

0

the premade objects you add use their type as the name,

Excuse me if I missed something, but the Sprite uses SPriteRenderer, which is different. I'm wondering how to find the official names in case they differ in the future

0

SpriteRenderer is a component. Canvas is an object. the Canvas object is called Canvas when you add it. SpriteRenderer has to be added to an object.

edit: just to clarify, the components use their type as their name as well. SpriteRenderer is called SpriteRenderer in the inspector. it will be attached to an object, though, so to create a reference to it you will first need to find the object and then GetComponent from that object.

0

Unity is a strange animal but once you wrap your head around the concept of game objects basically replacing a large amount of scripting it gets pretty easy to use. Hard to keep all of your game objects organised.