0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Could you post the code you're currently using? \$\endgroup\$ – DubGamer87 Jul 25 '17 at 7:13
1
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How can I create prefab for the ball? \$\endgroup\$ – Haileapp Jul 25 '17 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\$ – Tolga Şen Jul 26 '17 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\$ – Haileapp Jul 26 '17 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\$ – Tolga Şen Jul 26 '17 at 9:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.