I am making an angry bird type 2d game using Unity and C#. After I throw the ball from catapult, I have no idea on reloading the ball. Then I use SceneAsync to reload the ball but it is the worst thing I've ever made since I can't make the other sprite objects to continue their movents after the ball is thrown. So what code should I use to reload the balls after throwing?
1 Answer
\$\begingroup\$
\$\endgroup\$
4
You can use Instantiate method that unity provides for instantiating new gameobjects. Just keep a static reference for initial position like:
private static Vector3 initialPosition = new Vector3(10, 10, 0) //The values are dummy
And also keep a reference for the ball's prefab:
public Gameobject ballPrefab; //Drag the ball prefab in the editor
And finally when it comes to instantiate:
Gameobject newBall = Instantiate (ballPrefab, initialPosition, Quaternion.identity) as Gameobject;
Now the newBall will appear in the scene with the position you set with "initialPositon". And the newBall variable keeps reference for the new object that you have instantiated
-
\$\begingroup\$ How can I create prefab for the ball? \$\endgroup\$– HaileappCommented Jul 25, 2017 at 20:22
-
\$\begingroup\$ Just drag the ball object you created from the hierarchy, to the assets folder. Then it will become a prefabbed object. Remember: you need to drag prefab to the script's ballPrefab slot, not the gameobject from the hierarchy. Google these if you want to learn more "prefabbing an object", "Instantiating a prefab". \$\endgroup\$ Commented Jul 26, 2017 at 7:42
-
\$\begingroup\$ Thanks @Tolga! But after I add it,it says springjoint2d is missing..but I added it with..new ball.add Component<Springjoint2d>(); \$\endgroup\$– HaileappCommented Jul 26, 2017 at 8:08
-
\$\begingroup\$ Consider adding the joint manually in the inspector. That is a different problem and I cannot debug it with the comments since I cannot see your code. \$\endgroup\$ Commented Jul 26, 2017 at 9:44