Accessing Waypoint script property from Coordinate Labeler script

In this tutorial, we create an IsPlaceable property in the Waypoint script. And to access this property in the Coordinate Labeler script we use the following syntax:

Waypoint waypoint;

void Awake()
{
waypoint = GetComponentInParent();
}

However some textbooks mention creating an instance of a class in order to access that class members with the following syntax:

Waypoint waypoint = new Waypoint();

Are these equivalent ways of achieving the same result? Thank you.

Hi Guybrush,

Have you already tested your idea? If so, what was the result?

Hi Nina,

No, I have not tested my idea. I am more than halfway through this tutorial and up until now everything works properly so I don’t want to mess too much with the code by trying out different things. Because if the code gets messed up, I won’t be able to fix it up again so I’ll have to start all over. If it ain’t broke don’t fix it :slight_smile:
But I know we had a similar kind of conversation with you. I am almost done with the entire course and I still haven’t seen the use of the syntax Object object = new Object() anywhere so far. So my guess is this syntax is mostly used in pure C# programming and C# with Unity comes with many helpful pre-created code such as GetComponent and Unity properties and so on to simplify the coding process.

Oh, since we already had this discussion, do the following in your code: Type Waypoint test = new Waypoint(); at the top of your code. Save your script. Then run your game and check your console. Theoretically, there should be a message. And that message should answer your question why we don’t call constructors of classes deriving from Unity classes.

I typed what you suggested and I got the following message:
“You are trying to create a MonoBehaviour using the new keyword. This is not allowed. MonoBehaviours can only be added using AddComponent.”
Does this mean that all Unity classes deriving from MonoBehaviour cannot be created with the following syntax?
UnityClass instance = new UnityClass();
If that’s correct, than it looks like I finally got my answer but it is the first time we are discussing this with you. We never talked about this before. Thanks.

They can but it is not recommended because Unity usually creates these objects itself.

If you create a component anyway via a constructor, this is correct regarding “pure” C# but “incorrect” within the context of Unity. A component is not supposed exist without a GameObject object within the logic of Unity. You can easily break Unity’s logic by circumventing the rules of this framework.

We never talked about this before.

I know. I was referring to this: “we had a similar kind of conversation with you”. I assumed that we already talked about C# and programming, and your question was something like a follow-up question. :slight_smile:

Thanks a lot. This discussion made many concepts very clear :+1:

1 Like

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

Privacy & Terms