spawn GameObject at certain position

Started by
5 comments, last by welly_59 10 years ago

im trying to spawn a ball when the player collects a powerup in my breakout clone. But the problem i am having is that the gameobject spawns in the same place all the time.


public void SpawnBallClone()
    {
        Vector3 pos = GameObject.FindWithTag("Paddle").GetComponent<PaddleManager>().PaddlePosition();
        print(pos + "from the SpawnBallClone Method");
        Instantiate(Ball, pos, Quaternion.identity);
    }

the Vector3 pos is the correct value, i am using the print() to make sure the values are correct:

(5.7, -4.1, 0.0)from the SpawnBallClone Method

But when the Ball GameObject is instantiated it always instantiates at 0,0,0.

What am i missing here?

Advertisement

Looking at my initial ball spawn it seems i can also not affect the positioning of that either. On game start i am doing this:


public void SpawnBall()
    {

        StartCoroutine(WaitForBallCoroutine());
         
       
    }

    IEnumerator WaitForBallCoroutine(){
        Debug.Log ("about to yield return WaitForSeconds(1)");
        yield return new WaitForSeconds(1);


        Instantiate(Ball, new Vector3(0, 0, 0), Quaternion.identity);


    }
and whatever values i put into the ball instantiate it will always spawn at 0,0,0.
I am using the same code to instantiate the blocks at the beggining of a level and that works....so any tips on what is wrong/different would be great

    void CreateBlocks()
    {
        float XAxis = -6f;
        float YAxis = 4.5f;

        for (int rows = 0; rows < 5; rows++)
        {
            for (int columns = 0; columns < 13; columns++)
            {
                Instantiate(Block, new Vector3(XAxis, YAxis, 0), Quaternion.identity);
                XAxis = XAxis + 1f;
            }
            XAxis = -6f;
            YAxis = YAxis - 0.5f;
            Instantiate(Block, new Vector3(XAxis, YAxis, 0), Quaternion.identity);
        }

    }

sorted it, my start() contained a vector3 which was positioning it at 000. i removed that and created a call to the spawnmethod on creating the scene sending it a vector3 instead

sorted it, my start() contained a vector3 which was positioning it at 000. i removed that and created a call to the spawnmethod on creating the scene sending it a vector3 instead

Glad you figured it out. I didn't see anything in the code above that would have caused your issue. It is quite often that the offending code is often left out of posts where unexpected results are occurring.

Nonetheless, it is always a good idea to return to your posts with the solution, even if found by yourself. I commend you for that.

i had a script attached to the ball called ballcontroller. when i first started i wanted a quick way to get the ball in the middle of the screen so my ballcontroller.start() contained this line:

transform.position = new Vector2 (0, 0);

When i was instantiated a new ball the start() was moving it back to 0,0,0. What i have done is removed that and instantiated the ball using my gamecontroller script which is attached to main camera, creating a spawnball() in ballcontroller to handle new balls.

ive made this so difficult by not planning it all beforehand. Adding new features sometimes means i have to redo a lot of code . note to self for next time i suppose!


using my gamecontroller script which is attached to main camera

It would be more idiomatic to not have the game controller script attached to your main camera. Why should your camera be responsible for controlling your game?

Might as well create a game object, call it - Game Manager/Controller - attach the game controller script to that object. This object can hold all general components (ambient sounds, ball spawn locations, score/hiscore, things that are general for the game and not attached to a specific object.

Your camera should be for presenting the view. Just a little cleaner in my opinion.

thanks for that advice. i didnt think about creating an empty object!

This topic is closed to new replies.

Advertisement