Problem instantiating a user defined object at run time (c#)

I've created a Ball object which I'm trying to instantiate from another class called BallsQueue.cs and then change a property of the ball. Here's the method:

public void MakeABall(float laneNumber)
    {
        PackedSprite packedSprite = (PackedSprite)gameObject.GetComponent("PackedSprite");
        Ball ball;
        ball = Instantiate(Ball, new Vector3(laneNumber, 12.8f, 1f), Quaternion.identity) as Ball;

        if(ball == null)
            Debug.Log("ball is null");
        else
            ball.ballColor = BallColor.Green;           
    }

When I run this the ball is created and goes down the screen like it should. But I get a the Debug.Log message telliing me the Ball is null and I wasn't able to change the ballColor enum to BallColor.Green . I have 'public GameObject Ball;' at the top of the file and in Unity I've drag&dropped a Ball prefab into the Ball slot. Is this a casting problem?

Thanks, simesf

Just as a quick tip, you can eliminate one line of code by removing `Ball ball;` and putting `Ball` at the beginning of the next line.

However, I've never created a new GameObject by referring directly to its script type.

I would (personally) rewrite the instantiation to this:

GameObject ball = (GameObject)Instantiate(ballPrefab, new Vector3(laneNumber, 12.8f, 1f), Quaternion.identity);

Ensuring to link a prefab ball to a public GameObject named ballPrefab, of course.

Any further calls to alter the ball as a GameObject from here should be successful.

use Ball as the type instead

public Ball Ball;

Then you can drag your ball object onto it

One issue with doing that - it'll have issues if you don't set the referenced prefab to none in the inspector and back to the ball you want (as it'll try add it as a GameObject)

If you change the name of the variable instead (to ballPrefab maybe), it'll be fine