Fundamental Question

Hello, I have a really fundamental question here.

[RequireComponent (typeof(Ball))]
public class DragLuanch : MonoBehaviour {

	
	private Vector3 dragStart, dragEnd;
	private float startTime, endTime;

	private Ball ball;
	// Use this for initialization
	void Start () {

		ball = GetComponent<Ball>();
	}

	public void DragStart ()
	//Capture time and position of drag start
	{ dragStart = Input.mousePosition;
		startTime = Time.time;

for code above,

[RequireComponent (typeof(Ball))]

for this line, I realized that ‘Ball’ means the name of ‘script’ not the name of ‘component’ . I thought it should be name of component but not and was wondering if there’s reason for it?

Second,

private Ball ball;

from this line, Ball is the type of variable ball. I get that, but what is Ball here? meaning that does this mean the script Ball or class Ball? Can you explain me what that line means? and how it links to

ball = GetComponent();

because here Ball is also script name… I am bit confused on fundamental logic here I guess. Let me know! and thank you always.

I had trouble trying to explain the differences in just words so I have added a little bit of clarification to start.

A script file (so in this example Ball.cs) is just a text file that contains C# code that the computer reads. In general in Unity this file name must match the name of the class so Ball.cs will contain a class definition for “Ball” and House.cs will contain a class definition for “House”.

A Class in C# is a template or blueprint for how to create 1 or more objects that are called instances. The class definition below is a very basic class called Ball with only one public property of type Color. We can have more than one instance of a ball all with different colours but they all are built using the same template as defined by the Class.

public class Ball : MonoBehaviour
{
    public Color BallColor;   
}

The line of code
[RequireComponent (typeof(Ball))]
This is referring to the Class Ball that will need to be contained in a script file of the same name. So I suppose you can think of like saying If this class is attached to a GameObject make sure that there is also a script file attached that contains a definition for a Ball class.

This line of code
private Ball ball;
basically says I am going to have a thing called ball and it will look like the template defined in the class Ball.

This line is a little more involved
ball = GetComponent<Ball>();
you need to remember that when the game starts Unity creates all of the GameObject instances and as part of that it is creating instances of all of the attached components. So it creates a new ball object based on the Ball Class attached in the Ball.cs script and sets all of the parameters as they were defined in the editor properties window.
This line basically says return me that instance of a Ball class you created when the game started running.

I hope that makes some sense??? and perhaps helps a little

Hi Bevan_Calliess,

This helps a lot. I do think I feel more comfortable on it now. Thank you for your time on it!

Privacy & Terms