Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

I need Help I don't Know why this is not working im new to Unity So i may be under looking something

Discussion in 'Scripting' started by ExtWaffleBossAL, Jul 26, 2022.

  1. ExtWaffleBossAL

    ExtWaffleBossAL

    Joined:
    Jul 22, 2022
    Posts:
    2
    So I'm am trying to access a variable through 2 Scripts
    One script is called Add 1 per second.cs and the other one is called Coins_display.cs

    second script


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Add1persecond : MonoBehaviour
    {
    Coins_display coins_Display;
    public GameObject add1persec;
    void Awake()
    {
    coins_Display = add1persec.GetComponent<Coins_display>();
    }
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    add1persec.coinsAmount += 1;
    }
    }





    Coins_display script




    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Coins_display : MonoBehaviour
    {
    public static int coinsAmount;
    private Text coinsCounter;
    // Start is called before the first frame update
    void Start()
    {
    coinsCounter = GetComponent<Text>();
    coinsAmount = 0;
    }
    // Update is called once per frame
    void Update()
    {
    coinsCounter.text = "Coins: " + coinsAmount;
    if (Input.GetKeyDown(KeyCode.Space))
    {
    coinsAmount += 1;
    }
    }
    }


    I don't know why it's not working Please help!

    I don't Know How to use the tags so if it miss lead You sorry for that
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,102
    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, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    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/

    You may edit your post above.