flying, walking 2 script and 1 button

Hello,

i have found a way to fly around in my game.
now i want to have a button so i can switch between the 2 functions, if the button is true the FPC can only walk, if the button is not true, the FPC can fly around.

now i have the 2 scripts, but what kind of code to i need to activate or de-activate the code, and that by just clicking on a button

Assuming you have general walking enabled you can use this to toggle your flying script on and off.

var flyScript;
Awake(){
    flyScript = GameObject.Find("player").GetComponent(flyScript);
}
OnGUI(){
    if(GUI.Button(Rect(100,100,200,200), "Fly Toggle"){
    flyScript.enabled = !flyScript.enabled;
}

thank you,

i have try to use your code, and tweek it al little bit

var Thruster;
function Awake(){
    Thruster = GameObject.Find("player").GetComponent(Thruster);
}
function OnGUI(){
    if(GUI.Button(Rect(100,100,200,200), "Fly Toggle")){
    Thruster.enabled = !Thruster.enabled;
} 
}

but it will not work:

error: NullReferenceExecption : Object reference not set to a instance of a object Boo.Lang.RuntimeService.GetProperty(System.Object target, System.String name) Fly-button.OnGui (0 (as Assets/A-My-Scripts/Fly-button.js:7)

how to fix this problem?

i use the general FPSWalker script with gravity,
and a thruster script witch i found here.

Do both of the control systems function? I mean, if you disable the flight script, does the FPS walker work, and if you disable the FPSwalker does the flight script work?

If so, then you might focus controlling the state in each script, and then hook that state control up to an external script?

Alternatively, you might want to simply stop rendering and otherwise disable the FPSwalker object, and replace it with a flyer object when the player changes from one mode to the other.

Replace player with the name of the gameObject you have the scripts attached to or try just using this instead of player.

Yes, both of my script function well.

but how can i control the state,

i just don’t know how switch between the 2 scripts.

is’t it possible to change the FPS walker script, that i make my changes into that script. with a button attachto it, that changes between walking and flying?

your option, stop rendering en replace the First Person Controller, with another First person Controller, that not as simple as you say.

i still don;t how to fix this problem.

this is my walking script,

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime / 2;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

this is my flying script,

using UnityEngine;
using System.Collections;


public class ThrusterScript : MonoBehaviour {
   // declare the variables
   public float Speed = 9;
   public float Drag = 20;
   public float DragNoMovement = 50;
   const float  airDrag = 0F;

   void FixedUpdate () {
      // get the inputs
      float horizontal = Input.GetAxis ("Horizontal");
      float vertical = Input.GetAxis ("Vertical");
      float altitude = Input.GetAxis ("UpDown");

      // check to see if the user is moving
      bool userMoved = Mathf.Abs (horizontal) > 0.1F || Mathf.Abs (vertical) > 0.1F || Mathf.Abs (altitude) > 0.1F;

      // determine the force vector
              float x = horizontal * Speed;         
      float z = vertical * Speed;
      float y = altitude * Speed;
      rigidbody.AddRelativeForce (new Vector3 (x, y, z), ForceMode.VelocityChange);
     
      // apply the appropriate drag when moving
      if (userMoved)
         rigidbody.drag = Drag;
      else
         rigidbody.drag = DragNoMovement;
   }
   
   
   void Start () {
      if (rigidbody==null)
         gameObject.AddComponent ("Rigidbody");

      // don't let the physics engine rotate the character
      rigidbody.freezeRotation = true;
   }
}

i want to have een simple button, that can switch between these to scripts. I have 1 Person i want to control. When the game enters, this person must beable to walk, but with a simple click on a button he must beable to fly.

if i use the just 1 code the functions works, but when i use them both i only can walk, not fly.

is there anyway to convert this into 1 script, or is it possible to make one script go to sleep, or something like that?

with this code, i’m bealbe to delete the first walking script and at my flying script.

var body : Component ;
var icon : Texture2D ;
var ThrusterScript : Component ;
var Controller : Component;
var isWalking : boolean = true;

var body1 : Component;
var ThrusterScript1 : Component;
var Controller1 : Component;

function OnGUI () 
{
	if(GUILayout.Button(icon)) 
	{
		isWalking = !isWalking;
		
	if(!isWalking){
//	if(FPSWalkerScript==null)
	gameObject.Destroy(body);
	gameObject.Destroy(Controller);
	gameObject.AddComponent("ThrusterScript");
	Debug.Log("Flying");
	}
	else{
	gameObject.Destroy(ThrusterScript);
	gameObject.AddComponent("body1");
	gameObject.AddComponent("Controller1");
	Debug.Log("Walking");
	}
}
}

but i get a error, that Unity can not found a Component with a Class body1. thats right because i dont have such class.

I have 1 person with optie too walk, i have aan empty game object with all of the scrips attach to it.

why will this not work?

what a little change can make a different.

var body : Component ;
var icon : Texture2D ;
var ThrusterScript : Component ;
var Controller : Component;
var isWalking : boolean = true;

//var body1 : Component;
var ThrusterScript1 : Component;
//var Controller1 : Component;

function OnGUI () 
{
	if(GUILayout.Button(icon)) 
	{
		isWalking = !isWalking;
		
	if(!isWalking){
//	if(FPSWalkerScript==null)
	gameObject.Destroy(body);
	gameObject.Destroy(Controller);
	gameObject.AddComponent("ThrusterScript");
	gameObject.AddComponent("BoxCollider");

	Debug.Log("Flying");
	}
	else{
	[b]gameObject.AddComponent("FPSWalkerScript");
	gameObject.AddComponent(CharacterController);[/b]
	gameObject.Destroy(ThrusterScript);
	Debug.Log("Walking");
	}
}
}

but the next problem is, when i have destroy: Body and Controller, the script is missing component.

so this functions only will work 1 time.

is there a way to set the variable again, but then in runtime!?