hi i am working in an kind of cookieclicker game and i need the cookie float witch holds the amound of cookies in another script(to check if the float is 10)I had an idear how to do it but id didnt work so i was looking through unity forum, Yt tutorials, and random websides but nothing worked.
in my first Script named Cookiesplus is this float(and more but i dont think it matters in this case)
public float Cookies = 0;
And i the second Script(named Cookis10) is what i thougt was a solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
Referencing variables, fields, methods (anything non-static) in other script instances:
It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
public class Cookis10 : MonoBehaviour
{
public GameObject cookieJar;
Cookiesplus cookieJarScript;
void Start()
{
cookieJarScript = cookieJar.GetComponent<Cookiesplus>();
}
void Update()
{
if(cookieJarScript.cookies > 0)
{
debug.Log($"cookies in cookie jar are {cookieJarScript.cookies}");
cookieJarScript.cookies--;
}
}
}
What you’re trying to do is get the script on said object you declare. In this case I named it’s reference to “cookieJar” to make sense. The Start() method gets the script(component) and now saves that reference. Now for the life of the game you can constantly call to the cookieJarScript all you want.
thank you vervy much it worked but ine question what does cookieJarScript.cookies–; do.
because i wont to work on it and dont know if i need it if i just want to checkt how many coookies there are and then do something
cookies++;
// or
cookies--;
// is basically shorthand for
cookies = cookies + 1;
cookies = cookies - 1;
it just means increment or decrement(add 1 or minus 1). Or another way to type it, especially if you need more than one is:
cookies += 2;
cookies -= 5;
Whenever code looks more slim or sleek, is definitely is more slim and sleek for the compiler, thus making code faster. Less beating-around-the-bush, and all that.
I just put that in there, so you can see when you use that logic, your print method would be saying:
9 cookies left…
8 cookies left…
7 cookies left…
etc… That way you can see both that you can in fact see how many cookies there are, but also modify the number of cookies. A 2 for 1 special, hot on your plate!
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?