Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Get a float from another Object/script

Discussion in 'Scripting' started by xelle43, Aug 25, 2023.

  1. xelle43

    xelle43

    Joined:
    Jul 13, 2023
    Posts:
    5
    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;


    public class Cookis10 : MonoBehaviour{

    public GameObject Objectwithscript;
    Cookiesplus scriptName;
    void Start()
    {
    Cookies2 = Objectwithscript.GetComponent<Cookiesplus>().Cookies;
    }
    void Update()
    {
    if(Cookies2 =10){
    debug.Log("cookies are 10");
    }

    }

    }
    if the codes i dumb i had a better solution but this was the last i tryed


    I would be happy if someone knows a solution.
    if you have questions i will answer them as soon as i can.
    Xelle
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    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:

    http://plbm.com/?p=220

    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?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
    Ryiah likes this.
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Code (CSharp):
    1. public class Cookis10 : MonoBehaviour
    2. {
    3.     public GameObject cookieJar;
    4.     Cookiesplus cookieJarScript;
    5.    
    6.     void Start()
    7.     {
    8.         cookieJarScript = cookieJar.GetComponent<Cookiesplus>();
    9.     }
    10.    
    11.     void Update()
    12.     {
    13.         if(cookieJarScript.cookies > 0)
    14.         {
    15.             debug.Log($"cookies in cookie jar are {cookieJarScript.cookies}");
    16.             cookieJarScript.cookies--;
    17.         }
    18.     }
    19. }
    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.
     
    Ryiah likes this.
  4. xelle43

    xelle43

    Joined:
    Jul 13, 2023
    Posts:
    5
    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
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    any time there is:
    Code (CSharp):
    1. cookies++;
    2. // or
    3. cookies--;
    4. // is basically shorthand for
    5. cookies = cookies + 1;
    6. 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:
    Code (CSharp):
    1. cookies += 2;
    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. :)
     
    Ryiah likes this.
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    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! :cool:
     
  7. xelle43

    xelle43

    Joined:
    Jul 13, 2023
    Posts:
    5
    thank you :)
     
  8. xelle43

    xelle43

    Joined:
    Jul 13, 2023
    Posts:
    5
    One question how can i move a canvas or make it invisible?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Please start a new post for new stuff.

    Canvases are only movable conditional upon and in the specific context of their Render Mode.

    Most Unity objects become invisible when you set them either inactive or disabled.

    Make sure you understand the side effects of randomly deactivating / disabling different things.

    Beyond that, when you start a fresh post for this new question, keep this in mind:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    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?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  10. xelle43

    xelle43

    Joined:
    Jul 13, 2023
    Posts:
    5
    thank you