Combine 2 Scripts from Different Objects

I wanna combine 2 scripts from different objects. My first script like this;

//ScriptA
...
public void CastRay()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
     
        if (hit.transform.gameObject.tag == "BadGuy")    //this line give me "NullReferenceException: Object reference not set to an instance of an object" but it works. I can't get it.
        {
            gameController.AddScore(scoreValue);
            Destroy(hit.transform.gameObject);
        }
        else
        {
            gameController.LessScore(scoreValue);
            Destroy(hit.transform.gameObject);
        }
}

And my other script;

//ScriptB
...
public void ShowOnScreen(GameObject[] thisList)
    {
        for (int i = 0; i < thisList.Length; i++)
        {
            Instantiate(thisList[i], Vector3.Lerp(startPoint, endPoint, (float)i / (thisList.Length - 1)), Quaternion.identity);
        }       
        if (thisList == enemylist1)
        {
            Debug.Log("Enemy");
        }
        else
        {
            Debug.Log("No Enemy");
        }
    }

Both of them works fine but I wanna use “if statements” like this;

...

if (thisList == enemylist1 && hit.transform.gameObject.tag == "BadGuy")
       {
            gameController.AddScore(scoreValue);
            Destroy(hit.transform.gameObject);
        }
else 
       {
            gameController.LessScore(scoreValue);
            Destroy(hit.transform.gameObject);
       }
}

I have list of prefab more than one and they showing on screen randomly. I wanna customize every list. Thanks for helping. Yes I am newbie for C# and Unity ^^ By the way,sorry for my bad English :slight_smile:

Is it possible? :frowning: Nobody? I am really need help with that :frowning:

It’s not very clear what your problem is. Bad english is okay (we’ve all been there), but I can literally not get what you need help with. This:

"I have list of prefab more than one and they showing on screen randomly. I wanna customize every list. Thanks for helping"

No clue what that’s supposed to mean.

I the meantime, you’re getting a nullreference on your raycast because you’re not checking if you actually hit anyhting:

//ScriptA
...
public void CastRay()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
    //if you didn't hit anything, hit.transform will be null, so you need to check that:
    if(hit.transform != null) {

        if (hit.transform.gameObject.tag == "BadGuy") //won't give nullreferences anymore.
        {
            gameController.AddScore(scoreValue);
            Destroy(hit.transform.gameObject);
        }
        else
        {
            gameController.LessScore(scoreValue);
            Destroy(hit.transform.gameObject);
        }
    }
}
1 Like

Ok. I will try like this;
I have apple,car,pencil,tomato prefabs. I wanna show them on the screen like this;

enemylist1: tomato,pencil
enemylist2: car,apple,tomato
enemylist3: pencil,car,tomato,apple

I can do that with “ScriptB”. I show every prefabs on the screen. Tomato,pencil,car,apple. I can shoot them with “ScriptA” . Now when I wanna hit just tomato or pencil I need “if statement” like this;

...

if (thisList == enemylist1 && hit.transform.gameObject.tag == "Tomato" ) //or Pencil. or both of them.
       {
            gameController.AddScore(scoreValue); //if you hit tomato or pencil get 10 point.
            Destroy(hit.transform.gameObject);
        }
else
       {
            gameController.LessScore(scoreValue); //if you hit car or apple lose 10 point
            Destroy(hit.transform.gameObject);
       }
}

** @Baste Thanks for the “nullreferences” solution. Thank you very much.**

No solution for “if statement” ? Is it possible ? I wondering. ^^