Global namespace, Update, Start are recognised as variables

I am not able to run my game because it has a problem with the namespace the update and the start sections in my code

here is my score display script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class SocreDisplay : MonoBehaviour
{

    Text scoreText;
    GameSession gameSession;

    void Start()
    {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();
    }

    void Update()
    {
        scoreText.text = gameSession.GetScore().ToString();
    }

}

Hi Dave,

Do you have two scripts in your Assets folder with a SocreDisplay class in it? If not, please watch lecture “Fixing Visual Studio Problems” (currently #4).

I have watched lecture 4 and no I do not have ScoreDisplay 2 times in my scripts but now it’s having a problem with switching score text’s script to health script and health script is not working

Are there any other error messages in your console? And did you try what Rick suggested in lecture 4?

actually now that I have restarted my mac and opened up unity again I have new problems now the score display doesn’t work on the game over screen and now my player dies after only one hit

and now that the health works my player has 500 hundred health and takes one hit and goes to 400 but yet still dies

here’s my player code

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [Header("Player")]
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float padding = 1f;
    [SerializeField] int health = 100;
    [SerializeField] AudioClip deathSFX;
    [SerializeField] [Range(0, 1)] float deathSoundVolume = 0.7f;
  /*  [SerializeField] AudioClip shootSound;
    [SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
    [SerializeField] [Range(0, 1)] float; */ // for shoot sfx
    [Header("laser")]
    [SerializeField] GameObject laserprefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;
    

    Coroutine firingCoroutine;

    float xMin;
    float xMax;
    float yMin;
    float yMax;

    // Start is called before the first frame update
    void Start()
    {
        SetUpMoveBoundaries();
    }



    // Update is called once per frame
    void Update()
    {
        Move();
        Fire();  
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if(!damageDealer) { return; }
        ProcessHit(damageDealer);

    }

    private void ProcessHit(DamageDealer damageDealer)
    {
        health -= damageDealer.GetDamage();
        if (health <= 0)
            damageDealer.Hit();
        {
            Die();
        }
    }
    public int GetHealth()
    {
        return health;
    }

    private void Die()
    {
        FindObjectOfType<Level>().LoadGameOver();
        Destroy(gameObject);
        AudioSource.PlayClipAtPoint(deathSFX, Camera.main.transform.position, deathSoundVolume);
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
          firingCoroutine = StartCoroutine(FireContinuosly());
        }
        if (Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(firingCoroutine);
        }
    }

    IEnumerator FireContinuosly()
    {
        while (true)
        {
            GameObject laser = Instantiate(laserprefab, transform.position, Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
          //  AudioSource.PlayClipAtPoint(shootSound, Camera.main.transform.position, shootSoundVolume); for shoot sfx
            yield return new WaitForSeconds(projectileFiringPeriod);
        }
    }


    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
        transform.position = new Vector2(newXPos, newYPos);
    }
    private void SetUpMoveBoundaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;

    }
}

And after testing it looks like it does Die(); before checking the health weather its under or equal to 0

The issue with the Die method is here:

if (health <= 0)
   damageDealer.Hit();
{
    Die();
}

Can you spot it, too?

maybe DamageDealer is supposed to be with a capital D

What is the purpose of the { }?

oooooh, let me try

thank you that was it

but now I realise that the score is lost I make sure it has the same score script on it as the one in the shooting section which works but when they go to game over then they are treated with a a score of 0 no matter what

here is the score display script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class ScoreDisplay : MonoBehaviour
{

    Text scoreText;
    GameSession gameSession;

    void Start()
    {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();
    }

    void Update()
    {
        scoreText.text = gameSession.GetScore().ToString();
    }

}

Is the following line implemented in thh GameSession class?

gameObject.SetActive(false);

Actually I found the problem I reset the Game Session when loading Game over

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms