This might seem really basic, but I haven’t really found any good tutorials that focus on beginner stuff through scripting. (If you know any, please share!)
I just want to create a scene, and add a gameobject to it. (Like a simple rectangle).
How do I go about doing that?
public class SceneController : MonoBehaviour
{
Scene Scene1;
void Awake()
{
Scene1 = SceneManager.CreateScene("Scene1");
var rect = new Rect(0, 0, 100, 100);
}
}
Creating a scene through scripting is… “advanced” isn’t the right word, but it’s certainly unusual and a strange way of going about things. The fact that you seem to think it’s “beginner stuff” makes me suspect you think this is a common or normal way of creating scenes, which it isn’t. There are lots and lots of tutorials that focus on beginner stuff in Unity, and the fact that this isn’t covered by those tutorials should be taken as a clue. So, what’s your intention with creating this scene this way?
As far as creating a rectangle on screen, you’d most likely do that through a Canvas UI. Create a GameObject, attach a canvas, create a second GameObject, set it as a child of the first one, and add an Image component. All of this is drastically more straightforward to do in the editor rather than through scripting.
(The Rect class is just data, and not really used to draw rectangles on screen. I mean, you could use OnGUI and GUI.Box to draw one, but OnGUI is obsolete at this point, so don’t do that.)
I cheer you wholeheartedly on attempting to do it in scripting, since you can do pretty much anything you want in scripting, and I like to work this way myself, at least a lot of the time.
However as others point out above, it’s not the most common Unity3D workflow, and it’s quite advanced, and you won’t find nearly as many tutorials assuming you’re doing everything by scripting. The editor is there to help you… learn it!
That said, if you want to do it all in code, here’s an approach that will both give you great insight into Unity, as well as allow you to make small meaningful steps every single time you sit down to code.
only do one small thing at a time: add a GameObject only, see if that works. Now add a component. Did that work?
to see if something works, look in the Hierarchy window. Does that line up with what your code is doing?
if something unexpected happens when you try some step, back up and retry it and try to figure out where you went wrong. This is a GREAT point to post small code snippets here to ask pointed questions (be sure to use code formatting; see the first post), as well as perhaps screen captures of your hierarchy window (expanded), or your inspector window highlighting what you thing is wrong.
Finally, USE SOURCE CONTROL! Nothing is more frustrating than getting something working, then tweaking it and suddenly it doesn’t work, nobody can tell why it doesn’t work, changing it back doesn’t work, and you spend weeks chasing down a silly deletion or change that you didn’t ever mean to make. Put your entire project in git, there’s like a million tutorials, and make a fresh commit EVERY TIME SOMETHING WORKS! Seriously. You won’t regret having all those save points.