I have a gameobject called “Paddle”, this gameobject has the script PaddleScript attached to it.
I have another gameobject called “Brick”, this gameobject has the script BrickScript attached to it.
Both scripts reside in the MyGame namespace and are public classes.
Now PaddleScript has the following code:
namespace MyGame
{
public class PaddleScript : MonoBehaviour
{
int intScore = 0;
public void AddPoint(int scoreInput)
{
intScore = intScore + scoreInput;
}
{
{
This code works fine. (But I’m not sure if AddPoint is a method or a constructor.)
I want to access AddPoint from the script BrickScript.
Here are the two things I did, one works and the other one doesn’t work.
namespace MyGame
{
public class BrickScript : MonoBehaviour
{
int pointValue = 1;
void OnCollisionEnter( Collision col)
{
Destroy ( gameObject );
// Code that works.
GameObject.Find ("Paddle").GetComponent<PaddleScript>().AddPoint(pointValue);
// Code that doesn't work.
PaddleScript.AddPoint(pointValue);
}
}
}
My question is, why does the latter not work? Both classes are public and reside in the same namespace.