GUITexture Button Problem

Hello all,
well sometimes you get a piece of Wood in front of your Head…
… like me today…
so excuse me for this simple question maybe it’s too simple so that i don’t see the problem… but maybe you see where it hangs…
here is my problem:
I have a Gameobject Dog and a Gameobject with a GUITExture attached.
On my Gameobject Dog i have a C# Script where i handle the Acceleration Brake. So i made this public like this:
public bool brake = false;
public float accel = 0;
I can see the Values changing in the Inspector if i run the Game.
So i have now 2 GUITexture’s with a OnMouseDown Script attached. So if i touch one onMousedown brake = true, onMouseup brake = false the same for the accelleration.

private var ForeignObject: GameObject; 

function Start() { 

 ForeignObject = GameObject.Find("Dog");    

ForeignScript = ForeignObject.GetComponent("SteerScript");
 
}

function OnMouseDown( ) { 
//	Time.timeScale = 0.0;

	ForeignObject.brake(true);	
//ForeignObject.accell = 1;
	//Application.LoadLevel (0);    
}

I always got this error: ‘brake’ is not a member of ‘UnityEngine.GameObject’
What did i made wrong ? I have search the Do’s forums too but did not found any solution… :shock:

thank you

The problem is that your variables like brake and acceleration are members of the script you attached to the gameobject. So you would need to modify the variables on the script and not on the gameobject, like this:

ForeignScript.brake(true);

and not

ForeignObject.brake(true);

Guten Morgen Martin,
thxx for the quick reply.
Ja i saw that just after posting.
But i get also this error Unknown identifier: ‘ForeignScript’
Is there not a easy way to change a value on a other Gameobject ?
All solution i found here in the forum does’t work or did not cover my problem.
thank you

what i also try work so far but it doesn’t change the Value i want…

public var ForeignObject: GameObject; 

function Start() { 


ForeignObject = ForeignObject;    
 
ForeignScript = ForeignObject.GetComponent("SteerScript");
 

}

function OnMouseDown( ) { 

ForeignObject .SendMessage ("Gas", 1.0);
//ForeignScript.brake(true);	
	//Application.LoadLevel (0);
	   
}

So in my Gameobject attached Script i have this:

void Gas (float accel) {
		print (accel);
                accel = 1.0f;
				}

I receive there a 1 if i press my MouseButton on the GUITexture but that doesn’t change my accel Value in the same Script.

	void FixedUpdate () {
		
		float steer = 0;
		float accel = 0;
		bool brake = false;

		steer = Input.GetAxis("Horizontal");
		accel = Input.GetAxis("Vertical");

any idea ?
thxx

Hey from Hamburg! :slight_smile:

Well, I actually don’t use a lot C# hence I have not a good idea. Maybe a “using ForeignScript;” is missing. Personally I find it way easier to use JS. That would work in JS:

GameObject.Find(“SomeObject”).GetComponent(“SteerScript”).valueX = 123;

using is not needed for scripts

But you can not mix CS and JS in the same compile step, they won’t see each other at all.

So either one must be put in an earlier compile folder (standard asset, pro asset, plugin) or what I would highly recommend: decide which scripting language you use and only use that one. Mixing will lead to interesting (and in some cases unsolvable) problems.

Danke dreamora und auch Guten Morgen in die Schweiz !
That clears a lot of problems i had. Now the last script i had was this one in js.
So it’s the best to convert the Mouse Script to C#.
her is what i have but to access the value i’m not shure…

using UnityEngine;
using System.Collections;

public class Bremse : MonoBehaviour {

public GameObject ForeignObject;
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () { 
		if (Input.GetMouseButtonDown(0)){
			//	Debug.Log ();
			ForeignObject.GetComponent("SteerScript").accel = 1.0;
			}
		 		
		 		}
}

i got this message : UnityEngine.Component' does not contain a definition for accel’

Hi,
well still no success with it… hmm i don’t know what’s wrong.
I change all Script’s to C#. All are working well now except the one with the GUITexture.
Here is my Setup:

  1. GameObject Dog has a script(SteeringScript) attached for Steering Acceleration.
    If i use the Keyboard input in the Script for Steering etc. all work well.
    Now i want to use a GUITexture with a MouseDown Script for Set the Acceleration Value in the Script SteeringScript attached to the GameObject Dog.
    In my SteeringScript i have made this:
using UnityEngine;
using System.Collections;

public class SteeringScript : DogScript {
	
	public bool brake = false;
	public float accel = 0;

	void FixedUpdate () {
		
		float steer = 0;
//		float accel = 0;
//		bool brake = false;
		
		if ((checkForActive == null) || checkForActive.active) {
			steer = Input.GetAxis("Horizontal");
			accel = Input.GetAxis("Vertical");
			brake = Input.GetButton("Jump");
		}

Now on my GUITexture i have attached this Script:

using UnityEngine;
using System.Collections;

public class Bremse : MonoBehaviour {

public GameObject ForeignObject;

	// Use this for initialization
	void Start () {
	ForeignObject = GameObject.Find("Dog");

	}
	
	// Update is called once per frame
	void Update () { 
//		ForeignScript = ForeignObject.GetComponent("SteeringScript");
		if (Input.GetMouseButtonDown(0)){
				Debug.Log (ForeignObject);
			ForeignObject.GetComponent(SteeringScript).accel = 0.0f;
		//GameObject.Find("Dog").GetComponent("SteeringScript").brake(true);
			}
		 		
		 		}
}

It doesn’t matter what i try i get alway errors…
here is what i get now: UnityEngine.Component' does not contain a definition for accel’
hmmm what is wrong ? I#m shure i’m close…
thanxxx for the help

High all,
well problem is solved after 30h of trial error…
damn i was so close the hole time… :wink:
here is what i did.
I just wrote a new MouseDown Script with a Reference to my Steerscript like:

public SteerScript steer;

void OnMouseDown () { 

			steer.brake = true;
		 		
		 		}

sometimes it’s so easy …
Just assign the Script after in the inspector and you can forget all this GetComponent stuff…
greetz
Stefan