Time is too precious...

Yay! another whole day spent trying to code and absolutely nothing to show for it…

I wish I could get past this and actually do some artistic stuff. I find coding no fun whatsoever, I’d much rather be pushing polygons…

Some help? Im trying to use 3 guitextures as 3 buttons to change a float value on another object. Hours of forum searching just seriously spins me out…

    var object : Transform; 
    var RelevantTurnSpeed=100;

function OnMouseDown(){
	 var scriptVarible : float = object.GetComponent (SubSpeed).TheTurningSpeed; 

 scriptVarible=(RelevantTurnSpeed);
Debug.Log("buttonDown"+name);
}

So I’ve got 3 guitextures with this script on each. The public varibles for “relevant turnspeed” are set different in the inspector for each one.

Question1. Is the line directly under OnMouseDown ever going to work? I cant figure out if its working or not.

Question2. What would the code at the other end look like? I’ve tried :

private var TheTurningSpeed=1;
static var TheTurningSpeed=1;
and
(Public) var TheTurningSpeed=1;

To “Catch” the signal I’m sending, then I (hope to) use it in

  rigidbody.AddTorque (Vector3.up* TheTurningSpeed* Input.GetAxis ("Mouse X"));

I cant seem to find a way to inset debug code to detect what part is failing, which makes things 100% guesswork.

I’ve got the subz game, I know its not great, but once I get SOMETHING that works I could use the concepts for a spaceship shooter or dragons breathing fire or whatever, and get out of these code gutters. The problem is different computers are behaving different and I think this is the last problem getting content to work across all speeds of machine…

Any advice would be welcome relief and would really cheer me up…
Thanks…
AaronC :frowning:

For starters, if you’re writing a script that will directly address a specific script on another object, you should make the type of the variable which stores the reference to that script have the same type as the script. For example, instead of:

var object : Transform;

…write this:

var theOtherScript : TheOtherScript;

The name ‘TheOtherScript’ is literally the name of the script file minus the ‘.js’ part. The name ‘theOtherScript’ can be anything you like, but this kind of capitalisation convention (types begin with capitals, variables with lowercase letters) matches Unity’s API.

When you drag and drop the other object in the inspector, you’ll find that it will only let you drop objects which contain a TheOtherScript component, so that’s pretty useful in itself.

Beyond that, the big benefit is that you no longer need to use GetComponent with it because you already have a variable of the right type. Your script can then look like this:

var theOtherScript : TheOtherScript;
var relevantTurnSpeed = 100.0;			// I presume you want a float here, so I added .0

function OnMouseDown()
{
	theOtherScript.turningSpeed = relevantTurnSpeed;
}

The other script can then define turningSpeed like this:

var turningSpeed = 1.0;

The big mistake in your snippet of code was actually this:

var scriptVarible : float = object.GetComponent (SubSpeed).TheTurningSpeed; 
scriptVarible=(RelevantTurnSpeed);

You’re getting the value of another object’s variable and putting it into scriptVarible, but then you’re putting another value into scriptVarible, which overwrites it and does not update the TheTurningSpeed variable of the other script. To make that particular piece of code work, you’d have to write it like this:

object.GetComponent(SubSpeed).TheTurningSpeed = RelevantTurnSpeed;

…or if you need to store the value of TheTurningSpeed locally as well as assign to it:

var subSpeed = object.GetComponent(SubSpeed);
var scriptVarible = subSpeed.TheTurningSpeed;
scriptVarible = RelevantTurnSpeed + whateverElseYouWantToDo;
subSpeed.TheTurningSpeed = scriptVarible;

In that example, I’m putting the reference to the SubSpeed component into a variable so I only have to use GetComponent on it once, but as I mentioned above, the simplest solution is to just have a public reference to that component and drag and drop that in the inspector.

I hope this helps to clarify things. I should probably mention that I’m not a Javascript programmer, so there may be minor language-related errors in there, but I’m leaving those as an exercise for the reader. :wink:

Thats awesome Neil, thanks for all of that.

For starters, if you’re writing a script that will directly address a specific script on another object, you should make the type of the variable which stores the reference to that script have the same type as the script.

Thats a big help, I never knew that.

"The big mistake in your snippet of code was actually this:
Code:
var scriptVarible : float = object.GetComponent (SubSpeed).TheTurningSpeed;
scriptVarible=(RelevantTurnSpeed);
You’re getting the value of another object’s variable and putting it into scriptVarible, but then you’re putting another value into scriptVarible, which overwrites it and does not update the TheTurningSpeed variable of the other script."
I see what you mean. I’ve made two calls instead of one. So in hindsight, I can see that I first assigned “TheTurningSpeed” to the scriptVarible, in then the next line I assigned “RelevantTurnSpeed” to scriptVarible.

Did you say the second call never got executed? I would’ve thought the compiler would go through them top to bottom numerically, called one then called the next one below it…
Does the engine somehow hit the first varible assignment and then discard the second one because that call’s been sent or cached or similar?

Cheers
Aaron

P.S. Thanks for the new lease of life on this Neil. Its a big help. Im going to fiddle with it until it works.

The second line does get executed, but if you think about it, if you simplify your code, it looks like this:

var x = something.floatVariable;
x = 100.0;

This gets the value of something.floatVariable and puts it in x, then puts 100.0 in the same variable. It doesn’t put 100.0 in something.floatVariable at all. If you then return from the function, x is lost and you’ve effectively done nothing at all.