# Combine 2 Scripts from Different Objects

**URL:** https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505
**Category:** Scripting
**Tags:** Scripting
**Created:** [October 22, 2015, 3:01pm UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505 "2015-10-22T15:01:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fomgames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fomgames/32/380184_2.png) [@fomgames](https://discussions.unity.com/u/fomgames)
#### Post date: [October 22, 2015, 3:01pm UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505/1 "2015-10-22T15:01:47Z")

</div>

**_I wanna combine 2 scripts from different objects. My first script like this;_**

```csharp
//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;_**

```csharp
//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;_**

```csharp
...

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 🙂_**

---

<div class="post-metadata">

### Author: ![fomgames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fomgames/32/380184_2.png) [@fomgames](https://discussions.unity.com/u/fomgames)
#### Post date: [October 23, 2015, 12:47pm UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505/2 "2015-10-23T12:47:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Baste](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/baste/32/880289_2.png) [@Baste](https://discussions.unity.com/u/Baste)
#### Post date: [October 23, 2015, 1:08pm UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505/3 "2015-10-23T13:08:51Z")

</div>

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:

```csharp
//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);
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![fomgames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fomgames/32/380184_2.png) [@fomgames](https://discussions.unity.com/u/fomgames)
#### Post date: [October 23, 2015, 1:39pm UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505/4 "2015-10-23T13:39:39Z")

</div>

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;

```csharp
...

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.\*\*

---

<div class="post-metadata">

### Author: ![fomgames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/fomgames/32/380184_2.png) [@fomgames](https://discussions.unity.com/u/fomgames)
#### Post date: [October 24, 2015, 9:49am UTC](https://discussions.unity.com/t/combine-2-scripts-from-different-objects/602505/5 "2015-10-24T09:49:30Z")

</div>

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