Jump to content

C# Unity addition

Go to solution Solved by minibois,

remove the word 'Update' and replace it with 'OnMouseDown' (and remove the void OnMouseDown(); in your update function now)
This will make it so that every time the mouse will be clicked down, this function will play, which will do the cookie += 1; command.
 
Your code should become this:

using UnityEngine;using System.Collections; public class Clickable : MonoBehaviour {     public int cookie;    	// Use this for initialization	void Start () {		}		void OnMouseDown(){        cookie += 1;         }}


 
Edit:
to display the amount of cookies make a function called 'OnGUI'. In this you will make a GUI.Label in which you can display the variable 'cookie'.
Like this:
void OnGUI ()     {        GUI.Label(new Rect(10, 10, 100, 20), "Amount of cookies is: " + cookies);          }

@TheoWasTaken don't forget to follow your topics and quote people when you answer them. I hope my reply helped and if you have any more questions be sure to ask them

*To start this of I want to say I am completely new to programing. Been doing it for like 3-4 days.*

Im making a cookie clicker copy and I want to make as you press the cookie it gives you cookie (as a currency) and it gives me the messages:

error CS1547: Keyword `void' cannot be used in this context
error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='

 

 

then i also want it to display the current amount of cookies, any help on how to achieve that?

 

Scripts:

using UnityEngine;using System.Collections;public class Clickable : MonoBehaviour {    public int cookie;   	// Use this for initialization	void Start () {		}		// Update is called once per frame	void Update () {        void OnMouseDown();        cookie += 1;}}

Thanks

 

Link to comment
https://linustechtips.com/topic/486854-c-unity-addition/
Share on other sites

Link to post
Share on other sites

remove the word 'Update' and replace it with 'OnMouseDown' (and remove the void OnMouseDown(); in your update function now)
This will make it so that every time the mouse will be clicked down, this function will play, which will do the cookie += 1; command.
 
Your code should become this:

using UnityEngine;using System.Collections; public class Clickable : MonoBehaviour {     public int cookie;    	// Use this for initialization	void Start () {		}		void OnMouseDown(){        cookie += 1;         }}


 
Edit:
to display the amount of cookies make a function called 'OnGUI'. In this you will make a GUI.Label in which you can display the variable 'cookie'.
Like this:
void OnGUI ()     {        GUI.Label(new Rect(10, 10, 100, 20), "Amount of cookies is: " + cookies);          }

@TheoWasTaken don't forget to follow your topics and quote people when you answer them. I hope my reply helped and if you have any more questions be sure to ask them

"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 comment
https://linustechtips.com/topic/486854-c-unity-addition/#findComment-6534026
Share on other sites

Link to post
Share on other sites

You've got a function within a function.

Scripts:    using UnityEngine;    using System.Collections;         public class Clickable : MonoBehaviour {             public int cookie;                	// Use this for initialization    	void Start () {    	    	}    	    	// Update is called once per frame    	void OnMouseDown () {             cookie += 1;             }    }

DAYTONA

PROCESSOR - AMD RYZEN 7 3700X
MOTHERBOARD - ASUS PRIME X370-PRO
RAM - 32GB (4x8GB) CORSAIR VENGEANCE LPX DDR4-2400
CPU COOLING - NOCTUA NH-D14
GRAPHICS CARD - EVGA NVIDIA GEFORCE GTX 980Ti SC+ ACX 2.0 w/ BACKPLATE
BOOT and PROGRAMS - CORSAIR MP600 1TB
GAMES and FILES - TOSHIBA 2TB
INTERNAL BACKUP - WESTERN DIGITAL GREEN 4TB
POWER SUPPLY - CORSAIR RM850i
CASE - CORSAIR OBSIDIAN 750D

Link to comment
https://linustechtips.com/topic/486854-c-unity-addition/#findComment-6534038
Share on other sites

Link to post
Share on other sites

remove the word 'Update' and replace it with 'OnMouseDown' (and remove the void OnMouseDown(); in your update function now)

This will make it so that every time the mouse will be clicked down, this function will play, which will do the cookie += 1; command.

 

Your code should become this:

using UnityEngine;using System.Collections; public class Clickable : MonoBehaviour {     public int cookie;    	// Use this for initialization	void Start () {		}		void OnMouseDown(){        cookie += 1;         }}

 

Edit:

to display the amount of cookies make a function called 'OnGUI'. In this you will make a GUI.Label in which you can display the variable 'cookie'.

Like this:

void OnGUI ()     {        GUI.Label(new Rect(10, 10, 100, 20), "Amount of cookies is: " + cookies);          }

@TheoWasTaken don't forget to follow your topics and quote people when you answer them. I hope my reply helped and if you have any more questions be sure to ask them

 

thanks man! I left my computer and came back to a great answer. No errors anymore. Even I as a complete noob understod what you meant.

I have not tried the cookie counter yet, will respond if I happen to run in to new problems.

<3

Link to comment
https://linustechtips.com/topic/486854-c-unity-addition/#findComment-6534345
Share on other sites

Link to post
Share on other sites

thanks man! I left my computer and came back to a great answer. No errors anymore. Even I as a complete noob understod what you meant.

I have not tried the cookie counter yet, will respond if I happen to run in to new problems.

<3

:D

 

No problem. Good to hear my explanation was understandable. 

"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 comment
https://linustechtips.com/topic/486854-c-unity-addition/#findComment-6534396
Share on other sites

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

×