Managing multiple scripts

I have two scripts “Pickup” and “Throw”.

I noticed that they work differently depending on the order they are in, in the Inspector. Example:

If I have the “Throw” script before the “Pickup” script in the Inspector the “Throw” script no longer works.
If I have the “Throw” script after the “Pickup” script in the Inspector the two scripts work fine.

What are you guy’s methods of making it so scripts work no matter what order they are in?

Here are my two scripts

Pickup

public var pickupHeight:float = 100;
public var pickupSpeed:float = 10;
private var selected = false;
public function OnMouseOver(){
	if(Input.GetMouseButtonDown(0)) {
		selected = true;
	}
}

public function Update(){
	if (Input.GetMouseButtonUp(0)){
		selected = false;
	}
	if(Input.GetMouseButton(0)  selected){
		transform.position.y = pickupHeight * Time.deltaTime;
		var mp = Input.mousePosition;
		var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
		position = Camera.main.ScreenToWorldPoint(Vector3(mp.x, mp.y, screenSpace.z));
		transform.position.x = position.x;
		transform.position.z = position.z;
	}
}

Throw

// The power added to the xyz axises
public var power:float = 30;
private var lastFramePos:Vector3;
private var selected = false;
// If the user clicks on the object while over it
// Set selected to true (The object is selected)
public function OnMouseOver(){
	if(Input.GetMouseButtonDown(0)) {
		selected = true;
	}
}
// When the user lets go of the mouse calculate the amount of power needed to throw the object
public function Update(){
	// Get the current position of the object in this frame to calculate against last frame position
	var curFramePos = transform.position;
	// If the user releases the mouse button and the object is selected
	// Throw the object or set it down
	if(Input.GetMouseButtonUp(0)  selected){
		var x = (curFramePos.x - lastFramePos.x) * power;
		var y = (curFramePos.y - lastFramePos.y) * power;
		var z = (curFramePos.z - lastFramePos.z) * power;
		rigidbody.AddForce(Vector3(x, 0, z) * 1000);
		selected = false;
	}
	lastFramePos = transform.position;
}

I don’t get this, I add the the two scripts to my object test it, it works perfect!

I close Unity then restart it.

The scripts are still attached to the object, but the “Throw” script no longer works. I can not figure out why. Anyone know?

It sounds like you’ve got some kind of execution order dependency, and execution order of scripts isn’t guaranteed to be stable except for where you specify it in the project settings.

From a brief skim, it looks like both scripts read from and write to transform.position. Chances are that Pickup is clobbering the values such that Throw only ever gets a difference of 0, and thus does nothing.

Generally speaking, you want to make sure that only one script writes to a given variable. If that can’t be done, you need to structure things such that reading and writing happens in a consistent manner where the logic is preserved. For simple cases, shifting one to LateUpdate might do the trick.

I tried moving Throw to “LateUpdate” but that didn’t work, it just drops the object on release.

What I don’t get is that both scripts work together just perfect! But once I restart unity the Throw script no longer works unless I re-attach Throw to the object.

To me, that seems like it is a Unity bug, not a scripting bug, but maybe I am wrong…

It’s not a Unity bug - you just have scripts that are conflicting with each other.

A lot of what you have seems redundant between the 2 scripts. Why is a variable named selected stored in 2 spots? Why is the action that changes selected (OnMouseOver) done in 2 spots? You’re also trying to move the transform and apply a Ridigbody force at the same time.

Not every object you can throw, but it is possible to pick it up, so I thought it would be best to do it in two scripts, so if you want to do both you apply both if you want to do just one you apply just one, am I wrong? What is a better solution?

If it isn’t a Unity bug then can you explain why it works 100% until I restart Unity?

Probably has something to do with how Unity orders the execution of scripts. From what you’ve observed it would seem that it executes in the order you have added them in the current session but anything that was saved previously does not adhere to that rule. (The order is probably “undefined” which means you may observe some pattern but it’s never guaranteed.)

The main issue, again, is that you have “event” code across 2 scripts that is trying to influence the GameObject in different ways. Read the logic of your code aloud. “When I press the mouse button it will add force to the rigidbody and move the transform to a spot in the world.”

EDIT: If it’s just a config thing then you can combine it into a single script and just have variables called isPickUpable and isThrowable instead of trying to separate them.

That is what I was thinking of doing if I can’t have two scripts.

After combining the two scripts, It now works, even after I restart unity!

I also had to change:

lastFramePos = transform.position;

to this:

lastFramePos = curFramePos;