Jump to content

C# Unity | Same Int in multiple scripts?

Go to solution Solved by minibois,

I think in your first piece of code you can just change "if cookie > 50;" to "if Clickable.cookie > 50;". Meaning you access the int 'cookie' in the script 'Clickable' and like this get a value from another script.

*warning: I'm completely new to programing and my code will suck!*

So im creating a cookie clicker copy in C# Unity and I have come so far to create a clickable cookie that gives you a cookie. 
Now I want to create a clickable box that gives you a ''miner'' that will give you 1 cookie per second without clicking. And it will cost for example 50 cookies to buy one. And that is the problem, I made a new script that is supposed to when you press it give you a miner if you have more than 50 cookies (and remove them).

 

I get the message: The name "cookie" does not exist in the current context. 

 

Code: 

using UnityEngine;using System.Collections;public class Pointer : MonoBehaviour {    void OnMouseDown()    {        if cookie > 50;    }    // Use this for initialization    void Start () {}// Update is called once per framevoid Update () {}   }
The clickable cookie:
using UnityEngine;using System.Collections;public class Clickable : MonoBehaviour{    public int cookie;    // Use this for initialization    void Start()    {    }    void OnMouseDown()    {        cookie += 1;    }    // Update is called once per frame    void Update() {        Debug.Log (cookie);    }}

So basically I want to use the cookie int in the new script but I don know how...

Yes I know, Im a complete noob. Any idea on how to fix this? thanks <3

Link to comment
https://linustechtips.com/topic/487326-c-unity-same-int-in-multiple-scripts/
Share on other sites

Link to post
Share on other sites

I think in your first piece of code you can just change "if cookie > 50;" to "if Clickable.cookie > 50;". Meaning you access the int 'cookie' in the script 'Clickable' and like this get a value from another script.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

It's pretty easy, create a reference to the instance and call for the cookie variable through that reference point.
 

start out declaring the class variable

Clickable clickable;

Then create the reference point

void Start() {    clickable = this.getComponent<Clickable>();    //Or instead of "this", whatever gameobject you placed the Clickable script on, just be sure you have a gameobject to reference}

Then reference it like so:

void onMouseDown() {    clickable.cookie++;  }

I'm sure there are many other ways around it, but I like this one a lot. :)

Cheers,

Linus

Link to post
Share on other sites

I think in your first piece of code you can just change "if cookie > 50;" to "if Clickable.cookie > 50;". Meaning you access the int 'cookie' in the script 'Clickable' and like this get a value from another script.

 

nice to see you again  :D 

this was exactly what i was looking for thanks!

(do you know how to remove 50 from it? :D... I kinda think i know but it wouldnt change the cookie int) 

Link to post
Share on other sites

nice to see you again  :D 

this was exactly what i was looking for thanks!

(do you know how to remove 50 from it? :D... I kinda think i know but it wouldnt change the cookie int) 

So you want to remove 50 from the value contained in cookie, when it hits the 50 mark?

 

Like this:

if (Clickable.cookie > 50){cookie = cookie - 50;}

When cookie in the script 'Clickable' reaches 51, it removes 50 from it's value dropping it back to 1. If you want to make it drop down to 0 you should use >= instead of >, because that means greater than or equal to (compared to only greater than).

> would trigger the "cookie = cookie - 50;" when cookie's value is 51, while >= would trigger it at 50.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×