Help adding time to a countdown timer please?

Ok here are my scripts. I’m using C#, as you can tell.

I keep getting the error message "an object reference is required to access non-static member. Also a couple of others which I wasn’t getting before even thought I was doing the exact same thing.

Ok, here’s the script attached to the text:

usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;

publicclasstimer : MonoBehaviour {

publicfloatstartingTime;

TexttheText;

publicautoplayer;

voidStart () {

theText = GetComponent ();
}

publicvoidUpdate()
{
startingTime -= Time.deltaTime;

if (startingTime <= 0.0f)
{

}

theText.text = “” + Mathf.Round (startingTime);

}

publicvoidAddTime (inttimeToAdd)
{
startingTime += timeToAdd;
}

}

and here’s the script attached to the object I want to make the time increase when the player makes contact with it:

usingUnityEngine;
usingSystem.Collections;

publicclassdeathcore : MonoBehaviour {

publicinttimeToAdd;
//publicautogameController;
//publictimertimer;

voidOnTriggerEnter(Colliderother)
{
if (other.tag == “Player”)
timer.AddTime (timeToAdd);
Destroy (gameObject);

}
}

Any help would be much appreciated. Almost have a finished game but this is the last thing I need to do. Thanks.

By the way, obviously there are spaces after the publics etc just didn’t register when I pasted for some reason.

It would be better if you use [.code=CSharp][./code] (without dots) for displaying your code.
You should probably change your on trigger enter function to this:

void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
other.GetComponent <timer>().AddTime (timeToAdd);
Destroy (gameObject);

}
}
1 Like

Thanks for your reply. It lets me play it now but I get an error message when the player collides with the object I want to add the score, which reads:

NullReferenceException: Object reference not set to an instance of an object
deathcore.OnTriggerEnter (UnityEngine.Collider other) (at Assets/deathcore.cs:19)

post your new code so we can see what you have done.

Thanks for your reply. It’s exactly the same as before, just with the changes the user above suggested. :slight_smile:

Replace :voidOnTriggerEnter(Colliderother)
With this :voidOnTriggerEnter(Collider Colliderother)
You didn’t assign a type, so it remained as a null object, and because of that happens the not set to an instance of an object, and you should put your codes inside CODE tags, it’s easier for everyone to see, what the code does

1 Like

Yeah sorry I will in the future, just wasn’t sure how to as I haven’t posted here much. Thanks for your advice. I entered what you said and other text in my code turned red?

could you put in code tags what you think I should change it to? Thanks.

Sorry, I see now, that others fixed that, but is this script inside the GameObject with the text component, because if not, that’s the problem.

The second script I posted is just assigned to the object that I want to trigger the time to be added. Should I add it to the component with the text, too?

Okay, hope I found it, this part:

Is missing a curly bracket, so the if statement has no effect, and if anything collides with it, it will delete itself, and because he’s not really there anymore, but tries to delete itself again, it gives an error, replace:

this :
if (other.tag == "Player")
timer.AddTime(timeToAdd);
Destroy (gameObject);

with this:
if (other.tag == "Player") {
timer.AddTime(timeToAdd);
Destroy (gameObject);
}

Thanks. Just tried it and I get another error that goes:

Assets/deathcore.cs(23,23): error CS0120: An object reference is required to access non-static member ‘timer.AddTime(int)’

by the way, how do I post in code format?

Instead of public, set the AddTime function to static.

static void AddTime (inttimeToAdd)
{
startingTime += timeToAdd;
}

?

Just tried it and it made the AddPoints turn red and I got this message:

‘timer.AddTime(int)’ is inaccessible due to its protection level

I tried changing it to a static int too but got the “Assets/deathcore.cs(23,23): error CS0120: An object reference is required to access non-static member ‘timer.AddTime(int)’” message again

Set timetoadd static as well.

I get the Assets/deathcore.cs(23,23): error CS0120: An object reference is required to access non-static member ‘timer.AddTime(int)’" message again

oh corrected it says:

An object reference is required to access non-static member ‘time.startingTime’

Set that static too :smile:

ok just did that and then changed the text to static too cos it was coming up with an error telling me to do that too.

Now, I have an error which reads:

error CS0161: ‘timer.AddTime(int)’: not all code paths return a value

I think we’re getting somewhere with this, though. Thanks a lot. :slight_smile:

Did you, by any chance forgot to set it back to void?

ok I set it to a static public void. It now runs but my timer isn’t there anymore, doesn’t give me the option to change the initial starting time from within unity and the option to set the timeToAdd from within unity has gone, too. Hmm.