Compile error when trying to return Time.fixedDeltaTime to initialFixedDelta

On line 43 in the Resume method ( Time.fixedDeltaTime = initialFixedDelta ) I get a compile error "An object reference is required to access non-static member ‘GameManager.initalFixedDelta.’ As far as I can tell I have the same setup Ben has but I get this error. I see by the print statement that I am retrieving the initialFixedDelta of 0.2, but it won’t let me put it back in! I don’t know what I need to add here since Ben hasn’t added anything else ( as far as this part is concerned) either!
I would appreciate any and all help. Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class GameManager : MonoBehaviour {

	public bool recording = true;

	private bool paused = false;
	private float initialFixedDelta;


	void Start(){
		initialFixedDelta = Time.fixedDeltaTime;
		print (initialFixedDelta);
	}

	// Update is called once per frame
	void Update () {
		if (CrossPlatformInputManager.GetButton ("Fire1")) {
			recording = false;
		} else {
			recording = true;
		}

		if (Input.GetKeyDown (KeyCode.P) && paused == false) {
			paused = true;
			PauseGame ();
		} else if(Input.GetKeyDown (KeyCode.P) && paused == true) {
			paused = false;
			ResumeGame ();
		}
	}

	static void PauseGame (){
		Time.timeScale = 0;
		Time.fixedDeltaTime = 0;
	}

	static void ResumeGame (){
		Time.timeScale = 1;
		Time.fixedDeltaTime = initialFixedDelta;
	}
}

The problem is that you using an static method, the static method don’t know anything about the initialfixeddelta, since the initialfixeddelta is bound to instance, you have either to make the method non static or make the initial fixed delta static

2 Likes

Thanks for the reply, Joao! Yes, I am sadly ignorant of why my method is static and Ben’s isn’t. It looks the same to me! So I do not as yet know how to make my method non-static. Also, since the initialFixedDelta comes from the game’s preferences (?) I thought it would be naturally static. Again, I am confused since Ben does not use “private static float,” just “private float.” Please bear with me I am rather new to all of this.

1 Like

You simply remove the keyword static from your method declaration.

Looking at the github repository for the section, and the code changes specific for your current lecture, it doesn’t appear to be there;

2 Likes

Sure, no problem at all, this static concept is a bit complicated at first. The time.fixeddeltatime is static, but initialFixedDelta isn’t, here is the problem.

You could imagine everything that is static as another script accessible by all instances but don’t know anything about whatever isn’t static within those. The static method don’t have access to the initialFixedDelta, you could do:

A) make the method non static (just delete the static keyword from the method declaration): this would make the method bound to the current instance running the script, so it would have access to the initialFixedDelta.

B) make the initialFixedDelta static, this would make the initialFixedDelta shared by the static class.

C) make an static instance access to the class, it is a more advanced approach but would also fix this, it would be a bit overkill and unnecessary but is an option too: you could initialize one variable of type "static GameManager instance; " and under start address it to the current instance running the script by doing this: “void Start() { instance = this; }” (this is the keyword that represents the instance running the code). This implementation would allow the static class to have access to the instance, so you would be allowed to make this: “Time.fixedDeltaTime = instance.InitialFixedDelta;”.

1 Like

Wow! I’m so embarrassed. I have it there on PauseGame and ResumeGame but I could have sworn they weren’t there - even after I looked for why my method was being declared static. This was careless of me. Thank you, Rob.

2 Likes

Thank you again, Joao! It seems I had static on the method. Strange, as my brain seemed to completely ignore the fact it was there. I can’t even explain why I had it there in the first place. But type it I did and there it is. Thank you for your kind explanation. I will go with the simple suggestion A, but I will keep your detailed suggestions B and C inside my core memory for future reference. Thanks!

2 Likes

Nothing at all to be embarrased about, so don’t beat yourself up about it. It is easily done, especially if you are not 100% certain of specific syntax and maybe copy/paste a method to re-use and then change the inner workings.

Most people find that when they are really looking hard at some code, for ages, trying to work out what’s not quite right their eyes just get so used to seeing the same text the issue could literally appear as “I AM THE ISSUE” and we’d still not actually see it! :smiley: Code blindness… :slight_smile:

2 Likes

Code blindness! It’s such a perfect term it must be a thing! I admit that while I do not often copy/paste code from other sources (in order to get used code writing) I do find myself relying on auto completion (which kind of defeats the purpose of typing everything fresh). I think I meant to type public, hit the wrong key, didn’t see what I hit, Monodevelop threw a suggestion up, and I just went with it without really looking at what it was. Then surely I copy/pasted the Pause method to get the Resume method and never looked at the first word at the beginning of the code until you pointed that out his morning. Code blindness. Spot on.

2 Likes

hehehe…

Late nights, early hours of the morning… scrolling through pages of white… :slight_smile:

1 Like

Privacy & Terms