x


Changing aspects of a function mid play...

Ok, so thanks to you guys I got my last issue sorted..so hopefully you can help me on this one too.

I need to change the value of a function on the occurrence of an event happening in the game.

function calc () {
  var randomize    = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);

  randomize.Normalize();
  randomize *= randomness;

  flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
  flockVelocity = Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
  follow = chasee.transform.localPosition;

  flockCenter = flockCenter - transform.localPosition;
  flockVelocity = flockVelocity - rigidbody.velocity;
  follow = follow - transform.localPosition;

  return (flockCenter+flockVelocity+follow*2+randomize);

}

This is the code I have... I need to have flockCenter and flockVelocity change the script it's looking for. Problem is that the script is attached to another gameobject, how would I reference this gameobject? as I'm guessing the rest of the line would read very similar, just with the corrected script name...

Hopefully you can help, would not only be great for my game, but would also help me understand javascript a hell of a lot more!

more ▼

asked Nov 16 '09 at 03:38 PM

therussmorris gravatar image

therussmorris
51 8 9 16

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Instead of looking up the other gameobject by name as you do now (Controller.GetComponent("Boid Controller Flock 1")), (which is very slow by the way), you can add a public variable to your script, of type GameObject. This variable now shows up in the editor, and you can drag any gameobject on it that you want. A reference to that gameobject will then be put into your variable by Unity before your script starts.

var myBoid:GameObject;

A variable like that can also be changed based on some event. You can change it with script to be something else.

more ▼

answered Nov 16 '09 at 04:35 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
7.9k 19 43 85

(comments are locked)
10|3000 characters needed characters left

The getcomponent refers to the script, the controller refers to the object, so its only looking through that object..not everything, if I'm right. so its not too much of a performance hog. Because of this,I don't think your solution would solve my problem. I think I could be clearer though.. basically the controller and everything is already changing...

I just need to have the function look for a different script. The whole line EG ...

flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

can remain the same, I just want it to look for "Boid Controller" instead, as thats the name of the script applied to the changed controller...

hopefully that makes things simpler.

more ▼

answered Nov 16 '09 at 08:38 PM

therussmorris gravatar image

therussmorris
51 8 9 16

(comments are locked)
10|3000 characters needed characters left

I've added an if statement inside my function, so it changes the name of the script that it would refer to.. this is what I've done

function calc () {

var randomize = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);
randomize.Normalize();
randomize *= randomness;

         if(Input.GetKeyUp("c"))

{
flockCenter = Controller.GetComponent("Boid Controller").flockCenter;
flockVelocity =  Controller.GetComponent("Boid Controller").flockVelocity;
}

else

{
flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
flockVelocity =  Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
}

the script still works fine, but when I press C, I get the error that this line

flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

Object reference not set to an instance of an object

it seems that its not updating to look for the new script, and continues looking for the old script..

more ▼

answered Nov 17 '09 at 10:02 PM

therussmorris gravatar image

therussmorris
51 8 9 16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5068

asked: Nov 16 '09 at 03:38 PM

Seen: 871 times

Last Updated: Nov 20 '09 at 01:02 PM