- Home /
How to instantiate a new GameObject (sprite) properly?
I know of the Instantiate(gameObject) method, but I don't know how to make a reference to a gameObject. I have a sprite called redCircle in my game. I have a condition in my yellow circle script that says if there is a collision, there is a chance for a new red circle to spawn at a random location on the screen.
Here is my code for what I'm trying to do:
public var redCircle: GameObject; //this is at top of file
//Generate random pos on screen for red circle
screenPos.Set(Random.value * Screen.width,Random.value * Screen.height,10);
worldPos = Camera.main.ScreenToWorldPoint(screenPos);
worldPos.z = 0;
circleInstance = Instantiate(redCircle, worldPos, Quaternion.identity) as GameObject;
I have made my redCircle sprite a rigidBody2D and it has a box collider. When I hit a yellow circle, my game freezes for a half of a second and then keeps going with no new redCircle. The method I use for giving its location is the same as when move my yellow circle to a new position so it should be appearing on the screen.
Answer by ItsSonic · Nov 12, 2014 at 12:47 AM
Fixed the issue! Didn't select redCircle in the inspector.
Your answer
Follow this Question
Related Questions
Scripts wont work if deactivated then activated? 0 Answers
Sprite Not Displaying In-game 0 Answers
How can I instantiate multiple objects with a loop and have them be different 1 Answer
How can I make a 2d sprite shoot the direction player is facing? 2 Answers
Animator - Stoping && Playing from Specific frames. 5 Answers