Help Accessing another Script

Hi All,

I´m trying to do a Moon Landing Game. You control a spacescraft and have to activate different landing pads (depends on how much landing pads are available in the level). The Main Problem I have is accessing other Scripts, maybe someone can help me out here!?

I´ve commented the code so you can get an idea of what I´m trying to do… I have 4 scripts attached (InGameGUI, MainMenuGUI, PlayerControl, LandingPad)

InGameGUI

public class InGameGUI : MonoBehaviour {

	int numActivated;
	int totalLZ;

	public string guiMode = "InGame";
	
	void Update () 
	{
		if (Input.GetKeyDown ("escape")) // if Escape Key is pressed, switch to Pause State
		{
			Time.timeScale = 0; // Stop Time
			guiMode = "Paused";
			Debug.Log ("Paused..");
		}
	}
	public void OnGUI () 
	{
		if (guiMode == "Paused") // declares GUI Buttons if guiMode = Paused
		{
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 - 20, 150, 30), "Resume Game"))
			{
				Time.timeScale = 1; 
				guiMode = "InGame";
				Debug.Log ("resume..");
			}
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 + 20, 150, 30), "Quit to Main Menu"))
			{
				Time.timeScale = 1;
				Application.LoadLevel(0);
				Debug.Log ("quitting..");
			}
		}
		if (guiMode == "Win") // declares GUI Buttons if guiMode = Win
		{
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 - 20, 150, 30), "Next Level"))
			{
				Time.timeScale = 1;
				Application.LoadLevel(Application.loadedLevel+1);
				guiMode = "InGame";
				Debug.Log ("next Level..");
			}
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 + 20, 150, 30), "Quit to Main Menu"))
			{
				Time.timeScale = 1;
				Application.LoadLevel(0);
				Debug.Log ("quitting..");
			}
		}
		if (guiMode == "Lose") // declares GUI Buttons if guiMode = Lose
		{
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 - 20, 150, 30), "Retry Level"))
			{
				Time.timeScale = 1;
				Application.LoadLevel(Application.loadedLevel);
				guiMode = "InGame";
				Debug.Log ("retry Level..");
			}
			if (GUI.Button (new Rect (Screen.width / 2 - 75, Screen.height / 2 + 20, 150, 30), "Quit to Main Menu"))
			{
				Time.timeScale = 1;
				Application.LoadLevel(0);
				Debug.Log ("quitting..");
			}
		}
	}
	public void LZActivated() // check how many Landing Pads are activated. 
	{
		numActivated++;
		if (numActivated == totalLZ) // check if all Landing Pads are activated. If yes, change to Win State
		{
			Win();
		}
		Debug.Log ("LZ Activated");
	}
	public void Win() // declares what happens if GUI Mode is Win
	{
		Time.timeScale = 0;
		guiMode = "Win";
	}
	public void Lose() // declares what happens if GUI Mode is Lose
	{
		Time.timeScale = 0;
		guiMode = "Lose";
	}
}

MainMenuGUI

public class MainMenuGUI : MonoBehaviour {



	void OnGUI () 
	{
		if(GUI.Button(new Rect(Screen.width/2-75,Screen.height/2-20,150,30),"New Game"))
		{
			Application.LoadLevel(1);
			Debug.Log("Starting New Game");
		}
		if(GUI.Button(new Rect(Screen.width/2-75,Screen.height/2+20,150,30),"Continue Game"))
		{
			Debug.Log("Continue Game");
		}
	}
}

PlayerController - The Particle Emitter doesn´t work either, so I´ve commented it out, but that´s another story :smile:

public class PlayerController : MonoBehaviour {
	
	//  var bottomThruster : ParticleEmitter;
	//	var topThruster : ParticleEmitter;
	//	var leftThruster : ParticleEmitter;
	//	var rightThruster : ParticleEmitter;

	public GameObject shipExplosion;

	public void Update () 
	{
		if(Input.GetAxis("Horizontal") > 0) // checking for right arrow key
		{
			//		leftThruster.emit = true;
			rigidbody.AddForce(10,0,0);
		}
		if(Input.GetAxis("Horizontal") < 0) // checking for left arrow key
		{
			//		rightThruster.emit = true;
			rigidbody.AddForce(-10,0,0);
		}
		if(Input.GetAxis("Horizontal") == 0) // checking if no horizontal keys down
		{
			//		rightThruster.emit = false;
			//		leftThruster.emit = false;
		}
		
		
		if(Input.GetAxis("Vertical") > 0) // checking for up arrow key
		{
			//		bottomThruster.emit = true;
			rigidbody.AddForce(0,10,0);
		}
		if(Input.GetAxis("Vertical") < 0) // checking for down arrow key
		{
			//		topThruster.emit = true;
			rigidbody.AddForce(0,-10,0);
		}
		if(Input.GetAxis("Vertical") == 0) // checking if no vertical keys down
		{
			//		topThruster.emit = false;
			//		bottomThruster.emit = false;
		}
	}
	
	public void OnCollisionEnter(Collision collision) // Kill Game Object on Collision if Collision Magnitude higher than 2
	{
		Debug.Log(collision.relativeVelocity.magnitude);

		if (collision.relativeVelocity.magnitude > 2) // Kill Game Object if Collision higher than 2
		{
			Explode ();
		} 
		else if (collision.gameObject.tag == "LandingPad") // Debug Log if Collision with Landing Pad
		{
			Debug.Log ("Landed");
		} 
		else if (collision.gameObject.tag == "Asteroid") // Kill Game Object if collision with Tag Asteroid
		{
			Explode ();
		}
	}
	public void Explode() // Instantiate Ship Explosion
	{
		Instantiate(shipExplosion, transform.position, transform.rotation);
		Destroy(gameObject);
	}
}

Landing Pad

public class LandingPad : MonoBehaviour {

	public GameObject GuiMode;

	public void Start () 
	{
		GameObject.FindWithTag("GUI").GetComponent<InGameGUI>(); // try to get InGameGUI for every Landing Pad in Scene, so that I can setup Winning Conditions
	}

	void Update () {
	
	}

	public void OnCollisionEnter (Collision Collision)
	{
		if (Collision.relativeVelocity.magnitude < 2) // if Collision lower than 2, change Color to green
		{
			transform.renderer.material.color = Color.green;
		//	 GUI.LZActivated();
		}

	}
}

I would really appreciate if someone could help me out.

THANKS

Hi,

What exactly do you want to do?
And for the vfx, you need to .Start() and .Stop() your effect in order to see it or not. so you should do something like
So you could have an effect looping and you control it in script.

//Not tested
rightThruster.Start()

I can´t point with my finger at the problem but I think it´s located in the LandingPad Script.

Everything seems working fine, I can pause and resume the Game, exit to Main Menu and so on. The only thing I didn´t get working are the winning conditions. I want to script something like if all landing pads in this level are activated, change to Win State (as scripted in the InGameGUI).

I´ve read somewhere, that I have to do this by GUI.LZActivated(); → LandingPad.cs / row 19

The Function for LZActivated is declared in the InGameGUI script → InGameGUI.cs / row 67

I´ve tried the rightThruster.Start() but it didn´t work either. I´m using Particles from a downloaded asset…

In your Landing Pad Script you need this:

// variable declaration
// this is the reference to the script where your method LZActivated() is
InGameGUI inGameGUI;

// in Awake or Start
// you say they are all on the same GO? It should work that way. If it doesn't, you need to access it using FindGameObjectWithTag.
inGameGUI = GetComponent<InGameGUI>();

// Then OnCollisionEnter
// replace GUI.LZActivated();
inGameGUI.LZActivated();

I can’t test the syntax, but that should work. Please note that when you try to access a method or a variable, you need them to be public, otherwise you can’t. You could also make them static. In that case, you don’t need to create a reference but instead access them like that:

// Note that InGameGUI is the script name (class), not the variable here, and the method LZActivated() needs to be static.
InGameGUI.LZActivated();

For your effects, are they working correctly when you look at them in the editor?
Do you know if your effects are looping? I am not expert, but I have a scenario where I activate / deactivate them using Start and Stop.

Thanks really much for your help, I really appreciate it…

I changed the code and it works out so far. But now I have the problem, that as soon as I hit a LandingPad the Game Stops!?

The Particles working in the editor, but only if I select them in the Scene View…

I´d love to share the project but the file is too big to upload it :frowning:

Are you entering the Win condition?
I don’t know if you know how to use debug, but you can use that :

 Debug.Log("type your text here");

It can help you see what are the values when you enter a specific condition.
If your game stops, it’s probably because numActivated == totalLZ is true?

You should also check if OnCollisionEnter in your landing pad script is triggered more than once.

Are you Fx playing when you start your game in the editor? Are they spawned at the right location?
They could be disabled, or something else.