Home > Net >  I'm trying to get the sum of these two integers on a single script so I can have it be the text
I'm trying to get the sum of these two integers on a single script so I can have it be the text

Time:05-23

I am trying to get the integer "pop" from the first script and the integer "clack" to be added together so that they can become a text that is the score for my game.

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

public class BlueGun : MonoBehaviour

 {

public float bluedamage = 10f;
public float bluerange = 100f;
public Text ScoreText;
public int pop = 0;


public Camera fpsCam;

void start()
 {
  ScoreText.text = pop.ToString();
 }
void Update()
 {
    if (Input.GetButtonDown("Fire1"))
      Shoot();
 }

 public void Shoot ()
 {
  RaycastHit hit;
  if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, bluerange));
   {
    Blue blue = hit.transform.GetComponent<Blue>();
    if (blue != null)
     {
      blue.inquirehealth(bluedamage);
      pop  ;
      ScoreText.text = pop.ToString();
     }
   }
 }
}

This ^^ above is the integer for "pop." I understand that it turns it into a string at the end, making the adding aspect a bit more difficult. So maybe, I shouldn't do it that way. And So I am wondering how I can add these two integers on completely different scripts so I can convert them to a text for the score. I know how to do the conversion part, what I don't know is how to acquire the two integers to add them. Thank you.

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

public class GunRay : MonoBehaviour
{

public float damage = 10f;
public float range = 100f;
float fire;
public Text ScoreTextred;
public int clack = 0;

public Camera fpsCam;

    void start()
    {
      ScoreTextred.text = clack.ToString();
    }
    void Update()
    {
      if (Input.GetButtonDown("Fire1"))
        Shoot();
    }


     public void Shoot ()
    {
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range) && hit.transform.tag == "Red");
        {
            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
              target.TakeDamage(damage);
              clack  ;
              ScoreTextred.text = clack.ToString();
            }
        }

CodePudding user response:

Create a new cs file within your project. Add the namespace of your project to access their methods and variables.

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

//namespace of your project to call variables across different .cs files
using namespace_of_your_project;

public class GameManager {
var BlueGun = new BlueGun();
var GunRay = new GunRay();
int pop1 = BlueGun.pop;
int clack1 = GunRay.clack;
int popclack=0;

//create your sum() function here
sum_func() {
popclack = pop1   clack1;
  }
}

Then in an event etc call sum_func()

CodePudding user response:

You can create a 3. script (GameManager) and reach out to that script from BlueGun or GunRay,(i thought it would be a better practice to trigger the manager from other objects) whenever you want to update your score.

your GameManager script:

public class GameManager : MonoBehaviour
{
    
    
    int clack  = 0;
    int pop = 0;
    public Text ScoreText;
    public void updateScore(int? nClack, int? nPop)
    {
        clack = nClack ?? default(int);
            pop = nPop ?? default(int);
            int score = clack   pop;
            //update you score text in here
            ScoreText.text = score.ToString();
    }
}

your BlueGun script shoot method:

 public void Shoot ()
 {
  RaycastHit hit;
  if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, bluerange));
   {
    Blue blue = hit.transform.GetComponent<Blue>();
    if (blue != null)
     {
      blue.inquirehealth(bluedamage);
      pop  ;
      FindObjectOfType<GameManager>().updateScore(null, pop);
     }
   }

your GunRay script shoot method:

     public void Shoot ()
    {
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range) && hit.transform.tag == "Red");
        {
            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
              target.TakeDamage(damage);
              clack  ;
              FindObjectOfType<GameManager>().updateScore(clack, null);
            }
        }
  • Related