Can I use scene objects in VS directly?

Hey,
I’m loving this course so far but I have a concern. I come from a programming background and the Unity interface is my biggest obstacle so far. My main issue is in how we initialize the instance variables we created for AdventureGame.cs
We have to first create the objects in the scene from Unity, then declare their components in VS as instance variables, then initialize them by dragging objects in Unity, then define what these objects will do in VS. All this bouncing back and forth is honestly just horrible.
Is there a way to at least do the 3rd step inside VS by using the scene objects directly?
So basically instead of writing the 2 following lines of code and then initializing them through the Unity UI:
[SerializeField] Text textComponent;
[SerializeField] State startingState;

I instead write:
private Text textComponent = StoryText; // The text object created under canvas
private State startingState = StartingState; // The scriptable object created using the State class

It would make things significantly less confusing to me if I don’t have to bounce back and forth as often. I tried searching but couldn’t find an answer to this weird question of mine. Thanks and apologies for the hassle. Again, I really appreciate all the work you guys have put into your courses. Amazing work, really!

Hi @ZeronPallas,

Welcome to our community! :slight_smile:

Theoretically, you could do almost everything in code. You could create new game objects via code, add a component via code and give the game object a position via code.

The problem is: Your code will become fairly complex. The purpose of the Unity editor is to make the programming part less complex.

This won’t work. Unity internally creates a bunch of things behind the scenes when we create a game object. Furthermore, we cannot reference any objects at the top of the class in C#. The only exception is referencing an object by calling a constructor.

An approach which is generally considered as bad practice but which comes close to your idea is finding a game object via its name:

private Text textComponent;

void Start()
{
     textComponent = GameObject.Find("Name of my game object").GetComponent<Text>();
}

Our “starting state” does not exist in our scene. We would have to access the files. It’s not difficult but requires hard-coded strings as well.

In my personal opinion, it is better to get used to the Unity editor instead of typing a lot of code to get the same things done which we do within 2 seconds. We mainly use this game engine and its editor to save us time so we can create more complex games.

In the end, it is a matter of personal preference, though. Do what you feel is best for you. :slight_smile:


See also:

Thanks for the response. You’re absolutely right. I was expecting a much simpler coding solution but if it won’t take 2 seconds to implement in code, then I might as well use the 2 seconds to go through the Unity UI. Like you said, hardcoding strings is by far the worst option.

I tend to visualize a sort of flowchart/pseudocode in my head (and some times on paper too when I’m feeling responsible) whenever I’m working on a project. The whole bouncing back and forth thing doesn’t work for my process but I guess I’ll get used to it eventually. Thanks again!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms