Instantiating stuff :)

Hi guys, just got Unity 1.6 and can’t wait to try out the new features :slight_smile:

One thing I’m playing with today is instantiating objects. My character goes round the level collecting rings, which then add up in the GUI ring counter. Now, when he hits an enemy, I want him to loose those rings so the ring counter will reset to zero and all the rings he has are dropped from his position and 5 seconds later they vanish.

Now, I’ve created a rigidbody ring (The originals are not rigidbody as they float) and I’ve written the following script:

var ring : Rings;
var ringRB : Rigidbody;

function OnTriggerEnter (col : Collider) {
   if (col.name == "badnik") {
      if (!animation.IsPlaying ("spin"))
      Instantiate (ringRB)
}
}

Now, this works (yay!) however it only instantiates one ring in the same position as the original.

The “var ring: Rings;” code is connected to the Rings GUI code I have so I need some code to access that variable so it knows how many rings to create.

I also need help figuring out how to get the Ring GUI to go down to 0 rings at the same time. And to make the rings all emit from my character.

See: easy, lol! :shock:

Mike

Simply instantiating a Rigidbody won’t really do anything, as a rigidbody is a component, not a GameObject. You’ll want

var ringGO : GameObject;

Instantiate that, and then attach a rigidbody to it via AttachComponent(I think). If the object you’re instantiating has a RigidBody already, there’s no need to do anything besides instantiating the GameObject. Also, you need to feed Instantiate position and rotation values:

Instantiate(ringGO, transform.position, Quaternion.identity);

This will create the ring at the position of the object the script is attached to and with “zero” rotation.

as for the ring GUI, “var rings : Rings” is not what you want, I think. The best way to do it would be via static variables. In Rings.js, put:
static var rings = 0; (or whatever your initial value)
code in Rings.js can access this as it would any other variable, but being static means there is only one copy of this variable shared among any instance of the Rings.js script. I’m guessing this is what you want.

Then, any other script can simply access this global variable:

Rings.rings -= 1;

Quick correction.

These nothing wrong with

var ringRB : Rigidbody;

function OnTriggerEnter (col : Collider) {
   Instantiate (ringRB);   
}

it’s a perfectly legal, even elegant, way of instantiating an object. (See Object)

Thanks for the info, guys!

I added in the “transform.position” and “Quaternion”. So now each time he collides a ring object is created however it is created not at the centre of the character but at the base, and then instantly dissapears!

The ring needs to appear to come from the centre of the capsule collider and be thrown ever so slightly away from it to avoid collision woes.

I found some script examples that will throw them into a circle too, which would be fun.

It’s not yet connected to the GUI Text yet though. I’ll just explain my setup here a little more:

I’ve got a GUI Text object on screen. And a script attached to it with the GUI variable, and a bunch of behaviours written down (I think I’ve got the name wrong there) that add 1, 5, 10, and 25 to the counter.

Then I have a bunch of behaviours scripts elsewhere that link into that initial script. Are added to the character, and through triggers then add those amounts to the GUI Text. I was surprised how well I got it to work, lol! I have the same setup for score too.

Now, what I need to do with this instantiate script is on a collision with a certain object. if the animation isn’t playing, I need to instantiate the same amount of rings that is in the GUI Text and then reset the GUI Text amount to zero.

5-10 seconds later I want them to Destroy (Instantated.GameObjects) or whatnots.

I think I’ve explained it better this time…

Mike