Can someone help me fix this code please?

Hi,
I’m new to unity and am currently following a tutorial on how to make a FPS in unity.

I can not complete 1 step in the tutorial because it would appear that there’s an error in one of the js files in the tutorial I downloaded from the the Unity tutorial page.

These are the errors the console says there are :
Assets/Scripts/PerformanceTweaks.js(125,25):BCE0019: 'HasFPS’is not a member of ‘FPSCounter’.

Assets/Scripts/PerformanceTweak.js(129,39):BCE0019: ‘GetFPS’ is not a member of FPSCounter’.

Assets/Scripts/PerformanceTweak.js(260,41): BCE0019: ‘updateInterval’ is not a memver of FPSCounter’.

The code is:

var fpsCounter : FPSCounter;
var water : IslandWater;
var terrain : Terrain;
var messageTime = 10.0;
var scrollTime = 0.7;

private var messages = new Array();
private var times = new Array();
private var lastTime = 0.0;
private var doneNotes = false;
private var origDetailDist = 0.0;
private var origSplatDist = 0.0;
private var origTreeDist = 0.0;
private var origMaxLOD = 0;
private var softVegetationOff = false;
private var splatmapsOff = false;

private var lowFPS = 15.0;

private var skipChangesTimeout = 1.0;
private var nextTerrainChange = 0;

function Start()
{
	if( !fpsCounter || !water || !terrain ) {
		Debug.LogWarning("Some of performance objects are not set up");
		enabled = false;
		return;
	}
	
	origDetailDist = terrain.detailObjectDistance;
	origSplatDist = terrain.basemapDistance;
	origTreeDist = terrain.treeDistance;
	origMaxLOD = terrain.heightmapMaximumLOD;
	skipChangesTimeout = 0.0;
}

function Update ()
{
	if( !fpsCounter || !water || !terrain )
		return;
		
	if( !doneNotes  !Application.isEditor )
	{
		var hwWater = water.FindHardwareWaterSupport();
		if( hwWater == IslandWater.WaterMode.Simple )
		{
			AddMessage( "Note: water reflections not supported on this computer" );
			// in this case it also happens that terrain splat maps are not supported :)
			AddMessage( "Note: high detail terrain textures not supported on this computer" );
			splatmapsOff = true;
		}
		if( hwWater == IslandWater.WaterMode.Reflective )
			AddMessage( "Note: water refractions not supported on this computer" );
			
		var gfxCard = SystemInfo.graphicsDeviceName.ToLower();
		var gfxVendor = SystemInfo.graphicsDeviceVendor.ToLower();
		if( gfxVendor.Contains("intel") )
		{
			// on pre-GMA950, increase fog and reduce far plane by 4x :)
			if( hwWater == IslandWater.WaterMode.Simple )
			{
				ReduceDrawDistance( 4.0, "Note: reducing draw distance (old Intel video card detected)" );
			}
			
			softVegetationOff = true;
			QualitySettings.softVegetation = false;
			AddMessage( "Note: turning off soft vegetation (Intel video card detected)" );
		}
		else if( gfxVendor == "sis" )
		{
			softVegetationOff = true;
			QualitySettings.softVegetation = false;
			AddMessage( "Note: turning off soft vegetation (SIS video card detected)" );
		}
		else if( gfxCard.Contains("geforce")  (
			gfxCard.Contains("5200") || gfxCard.Contains("5500") || gfxCard.Contains("6100") || hwWater == IslandWater.WaterMode.Simple) )
		{
			// on slow/old geforce cards, increase fog and reduce far plane by 2x
			ReduceDrawDistance( 2.0, "Note: reducing draw distance (slow GeForce card detected)" );
			
			softVegetationOff = true;
			QualitySettings.softVegetation = false;
			AddMessage( "Note: turning off soft vegetation (slow GeForce card detected)" );
		}
		else
		{
			// on other old cards, increase fog and reduce far plane by 2x
			if( hwWater == IslandWater.WaterMode.Simple )
			{
				ReduceDrawDistance( 2.0, "Note: reducing draw distance (old video card detected)" );
			}
		}
		
		skipChangesTimeout = 0.0;
		doneNotes = true;
	}
	
	DoTweaks();
	
	UpdateMessages();
}

function ReduceDrawDistance( factor : float, message : String )
{
	AddMessage( message );
	var underwater : UnderwaterEffects;
	underwater = FindObjectOfType(UnderwaterEffects);
	RenderSettings.fogDensity *= factor;
	if( underwater )
	{
		underwater.uDensity *= factor;
		underwater.aDensity *= factor;
	}
	Camera.main.farClipPlane /= factor;
}

function OnDisable()
{
	QualitySettings.softVegetation = true;
}

function DoTweaks()
{
	if( !fpsCounter.HasFPS() )
		return; // enough time did not pass yet to get decent FPS count		
	
	var hwWater = water.FindHardwareWaterSupport();
	var fps : float  = fpsCounter.GetFPS();
	
	// don't do too many adjustments at time... allow one per
	// FPS update interval
	skipChangesTimeout -= Time.deltaTime;
	if( skipChangesTimeout < 0.0 )
		skipChangesTimeout = 0.0;
	if( skipChangesTimeout > 0.0 )
		return;
	
	// water tweaks
	var curWater = water.GetWaterMode();
	if( fps > 35.0 )
	{
		// frame rate high, use refractions | reflections
		if( hwWater == IslandWater.WaterMode.Refractive  curWater < IslandWater.WaterMode.Refractive ) {
			water.m_WaterMode = IslandWater.WaterMode.Refractive;
			AddMessage( "Framerate high, turning water refractions on" );
			return;
		}
		if( hwWater == IslandWater.WaterMode.Reflective  curWater < IslandWater.WaterMode.Reflective ) {
			water.m_WaterMode = IslandWater.WaterMode.Reflective;
			AddMessage( "Framerate high, turning water reflections on" );
			return;
		}
	}
	else if( fps > 25.0 )
	{
		// frame rate sort of high, can use water reflections
		if( hwWater > IslandWater.WaterMode.Simple  curWater == IslandWater.WaterMode.Simple ) {
			water.m_WaterMode = IslandWater.WaterMode.Reflective;
			AddMessage( "Framerate ok, turning water reflections on" );
			return;
		}
	}
	else if( fps < 10.0 )
	{
		// frame rate very low, turn water to simple
		if( curWater > IslandWater.WaterMode.Simple ) {
			water.m_WaterMode = IslandWater.WaterMode.Simple;
			AddMessage( "Framerate very low, turning water reflections off" );
			return;
		}
	}
	else if( fps < 25.0 )
	{
		// frame rate sort of low, use reflections only
		if( curWater > IslandWater.WaterMode.Reflective ) {
			water.m_WaterMode = IslandWater.WaterMode.Reflective;
			AddMessage( "Framerate low, turning water refractions off" );
			return;
		}
	}
	
	
	// terrain tweaks
	if( fps > 25.0 )
	{
		// bump up!
		++nextTerrainChange;
		if( nextTerrainChange >= 4 )
			nextTerrainChange = 0;
			
		if( nextTerrainChange == 0  terrain.detailObjectDistance < origDetailDist )
		{
			terrain.detailObjectDistance *= 2.0;
			if( !softVegetationOff )
				QualitySettings.softVegetation = true;
			AddMessage( "Framerate ok, increasing vegetation detail" );
			return;
		}
		if( nextTerrainChange == 1  !splatmapsOff  terrain.basemapDistance < origSplatDist )
		{
			terrain.basemapDistance *= 2.0;
			AddMessage( "Framerate ok, increasing terrain texture detail" );
			return;
		}
		if( nextTerrainChange == 2  terrain.treeDistance < origTreeDist )
		{
			terrain.treeDistance *= 2.0;
			AddMessage( "Framerate ok, increasing tree draw distance" );
			return;
		}
		if( nextTerrainChange == 3  terrain.heightmapMaximumLOD > origMaxLOD )
		{
			--terrain.heightmapMaximumLOD;
			AddMessage( "Framerate ok, increasing terrain detail" );
			return;
		}
	}
	if( fps < lowFPS )
	{
		// lower it
		++nextTerrainChange;
		if( nextTerrainChange >= 4 ) {
			nextTerrainChange = 0;
			lowFPS = 10.0; // ok, this won't be fast...
		}
			
		if( nextTerrainChange == 0  terrain.detailObjectDistance >= origDetailDist / 16.0 ) {
			terrain.detailObjectDistance *= 0.5;
			QualitySettings.softVegetation = false;
			AddMessage( "Framerate low, reducing vegetation detail" );
			return;
		}
		if( nextTerrainChange == 1  !splatmapsOff  terrain.basemapDistance >= origSplatDist / 16.0 )
		{
			terrain.basemapDistance *= 0.5;
			AddMessage( "Framerate low, reducing terrain texture detail" );
			return;
		}
		if( nextTerrainChange == 2  terrain.treeDistance >= origTreeDist / 16.0 )
		{
			terrain.treeDistance *= 0.5;
			AddMessage( "Framerate low, reducing tree draw distance" );
			return;
		}
		if( nextTerrainChange == 3  terrain.heightmapMaximumLOD < 1 )
		{
			++terrain.heightmapMaximumLOD;
			AddMessage( "Framerate low, reducing terrain detail" );
			return;
		}
	}
}

function AddMessage( t : String )
{
	messages.Add( t );
	times.Add( messageTime );
	lastTime = scrollTime;
	skipChangesTimeout = fpsCounter.updateInterval * 3.0;
}

function UpdateMessages()
{
	var dt = Time.deltaTime;
	for( var t in times )
		t -= dt;
	while( times.length > 0  times[0] < 0.0 ) {
		times.Shift();
		messages.Shift();	
	}
	lastTime -= dt;
	if( lastTime < 0.0 )
		lastTime = 0.0;
}

function OnGUI()
{
	var height = 15;
	var n : int = messages.length;
	var rc = Rect( 2, Screen.height - 2 - n * height + (lastTime/scrollTime*height), 600, 20 );
	for( var i = 0; i < n; ++i )
	{
		var text : String = messages[i];
		var time : float = times[i];
		var alpha = time / messageTime;
		if( alpha < 0.2 )
			GUI.color.a = alpha / 0.2;
		else if( alpha > 0.9 )
			GUI.color.a = 1.0 - (alpha-0.9) / (1-0.9);
		else
			GUI.color.a = 1.0;
		
		GUI.Label( rc, text );
		rc.y += height;
	}
}

Please reply if you have any solutions :slight_smile:

This errors tells me that this example is far too advanced for you and that you should start with something more simple. You even have not recognized that the error is not only in the script you postet.

When you really want to learn to program start with beginner tutorials, if you just want working scripts just ask directly if anyone is kind enough to make one for you.

It is meant to be a tutorial for beginners, everything was in one zip folder, including the scripts etc. and is all just supposed to work. It says that it teaches the basics and that all tools are already provided.

http://unity3d.com/support/resources/tutorials/fpstutorial.html

So your rudeness is not welcome and I will ignore any future posts by you.

There are 2 zip files for this…
The starting point which you have to build up yourself while following the instructions in the pdf files and the “complete” zip.

If you are following the pdf file instruction it is highly likely that errors will appear as you build up the code as some elements/objects have to be dropped in place before they work.

Your assumption that it “is all just supposed to work” isn’t true in this case.

You don’t say at what part in “Step 1” you have reached - have you reached Page 4 yet or is the error occurring after you have started to modify the scene?

I’m following a tutorial on youtube using this tutorial from unity as a base for his. I downloaded the complete zip with everything in, and imported everything in. I’ve reached a point where I’m trying to add the script to control the machine gun but it won’t let me drag and drop it on because there’s an error in another script which I’m guessing is this one that I posted.

I’m using the FPS_Tutorial.zip not the FPS_Tutorial_Completed.zip

To come back to the main subject, the script is trying to access a component (variable or function) that do not simply exist (the errors messages say it).

Multiple possibilities :

  • You erase the code lines.
  • The author made an error.
  • Something is missing from the Start, and you have to follow furthermore the tutorial.

In the worst case, you have to fix it by yourself.

I can’t see a PerformanceTweak.js script in the zip file for the FPS tutorial.

Could it be that this is from some other tutorial you may have tried?

The FPSCounter script doesn’t have the stuff that the PerformanceTweak script wants from it. So you’re not using the right version of one or both of the scripts.

–Eric

FPSCounter from FPS Tutorial

private var show = false;

function Update () {
	if (Input.GetKeyDown("0"))
		show = !show;
	if (show)
	{
		if (Time.timeScale != 0.0)
		{
			var fps : int = 1.0 / Time.deltaTime;
			guiText.text = fps.ToString();
		}
		else
		{
			guiText.text = "0";
		}
	}
	else
	{
		guiText.text = "";	
	}
}

@script RequireComponent(GUIText)

+1

No this is the first tutorial I’m following.

If I add in the code that you wrote, will that fix the problem?

What I “wrote” (copy paste is more precise) is simply the code from FPSCounter, from the same zip you downloaded. So, copy pasting it won’t fix the thing.

I don’t understand, I don’t remember having used such a script as PerformanceTweak during that tutorial. Why would you use that ?

I think that it came from the previous scene which was the Island one that is opened when Unity first starts up.
In the tutorial it said create a new scene instead of new project. So I’m guessing that it was from the Island scene as I said.

The basics of a first person shooter, yes.

I was talking about the basics of programming.

Were do i wrote anything rude? Did i wrote anything that was not true? Where there any offenses?

There were no errors in the code that is why i said that you should learn programming and your reaction shows me that you do not want to learn to help yourself.

Do I indicate that I don’t want to learn programming? No.
Do I indicate that I don’t want to help myself? No, I’m following this tutorial to help me learn to use unity, because all the scripts are already made. This tutorial is meant to help get used to the basics first, thats why all the scripts are already there.
And I don’t know if its because of your horrible grammar in the English language or not, but yes, the way you write does come out as offensive and demeaning to me, especially since this is an error not caused by me, or one that I could’ve prevented or avoided.

Now back to the topic. If there are no errors in the code, why does Unity say that there are compile errors, and the console indicates 3 specific errors in the code, with the lines where the errors are located.

Just get rid of that PerformanceTweaks script that has no reason to exist in the FPS Tutorial. When you do things irrelevant to the current project, start a new project, instead of working in the current project.

Last thing :

PerformanceTweaks has obviously connections with other pre existing components. Since they do not exist, there are, of course, errors messages (since you did not also import all scripts).

Merci Akilae,
I’ll just try again in a complete new project seeing that the script giving the problems wasn’t actually a part of the FPS tutorial.

OT: i must admit i find very amusing and interesting your quotes, AkilaeTribe. :slight_smile:

@Pasko : :wink: