Accessing another script on the same game object?

I have been playing around with writing my first script from scratch. Being a drag racer in the real world a nitrous controller seemed to be a good place to start. Being keen on realism and my eventual goals I did not want to just had HP.

The largest problem I am having is accessing the rpm from the drivetrain script in the CarTutorial. There are a few examples that tried from the GetComponent description page with no success. Its quite possible I am not implementing it correctly but its not from lack of effort.

Currently I receive the following error…

error CS0103: The name `rpm’ does not exist in the current context

Any help would be appreciated!

using UnityEngine;
using System.Collections;

// This class is used to simulate a nitrous system.

public class NitrousFogger : MonoBehaviour {
	
	//This setting arms/purges the nitrous system after the burnout.

	//This setting activates the nitrous system at a preset RPM range.
	public float ActivateRPM = 4000;
	public float DeactivateRPM = 6000;
	
	//Nitrous parameters adjustable by user
	public float NitrousOrificeSize = 0.042f;	
	public float NitrousPressure = 950;
	
	//Fixed properties for nitrous flow calculation	in kg m^3.
	private float NitrousFlowCoefficient= 0.70f;	
	private float NitrousDensity = 1222.7f;
	
	//Fuel parameters adjustable by user
	public float FuelOrificeSize = .034f;
	public float FuelPressure = 6.0f;
	
	//Fixed properties for fuel flow calculation in kg m^3.
	private float FuelFlowCoefficient = 0.75f;
	private float FuelDensity = 715.0f;
		
	//This activates the nitrous system.	
	private bool CalcActivation ()
	{
		bool result;
			if(rpm > ActivateRPM & rpm < DeactivateRPM & Input.GetButton ("NitrousPB1"))
				result = true;
			else
				result = false;
			return result;
	}
		
	// Calculate nitrous flow
	private float CalcNitrousFlowFoggerOne  ()	
	{
		float result;					
			if(Input.GetButton("NitrousPB1"))			
				result = ((((NitrousOrificeSize*NitrousOrificeSize*0.25f)*3.14159f)*NitrousFlowCoefficient)*Mathf.Sqrt(2*(NitrousPressure*689.47f)/NitrousDensity));
			else 
				result=0;		
		return result;
	}
	
	// Calculate fuel flow
	private float CalcFuelFlowFoggerOne ()	
	{
		float result;
			if(Input.GetButton ("NitrousPB1"))
				result = ((((FuelOrificeSize*FuelOrificeSize*0.25f)*3.14159f*.00064516f)*FuelFlowCoefficient)*Mathf.Sqrt(2*FuelPressure*689.47f/FuelDensity));
			else 
				result=0;
		return result;
	}
	
	// Is nitrous flowing?
	float FoggerOneNitrousFlow ()
	{
		float result;
			if(CalcActivation ())
				result = CalcNitrousFlowFoggerOne ();
			else
				result=0;
			return result;
	}
		
	// Is fuel flowing?
	float FoggerOneFuelFlow ()
	{
		float result;
			if(CalcActivation ())
				result = CalcFuelFlowFoggerOne ();
			else
				result=0;
			return result;
	}	
}

I can’t see you declaring the variable rpm anywhere is this script, but your trying to access if on line 34?

Would suggest:

otherScript = GetComponent<otherScript>();


if(otherScript.rpm > ActivateRPM & otherScript.rpm < DeactivateRPM & Input.GetButton ("NitrousPB1"))
          result = true;
         else
          result = false;
         return result

//This activates the nitrous system.
private bool CalcActivation ()

	{

		bool result;
		
		      	Drivetrain = GetComponent<Drivetrain>();
    			
				if(Drivetrain.rpm > ActivateRPM & Drivetrain.rpm < DeactivateRPM & Input.GetButton ("NitrousPB1"))
    				result = true;
    			else
    				result = false;
		
    	return result;
	}

With the above code I am getting the following errors…

(39,25): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

(41,47): error CS0120: An object reference is required to access non-static member `Drivetrain.rpm’

I have played around with for an hour or so and could not get Unity to play along. Sorry if I am missing something simple, the help is appreciated though.

//This activates the nitrous system.
private bool CalcActivation ()
{

bool result;
 
Drivetrain myComp = GetComponent<Drivetrain>();
 
if(myComp.rpm > ActivateRPM && myComp.rpm < DeactivateRPM && Input.GetButton ("NitrousPB1"))
result = true;
else
result = false;
 
return result;
}

Fixed

MyType varName = GetComponent<MyType>();

if(condition **&&** anothercondition){ 
//do stuff
}