[Solved] Need Help With Destroying Blocks With 2 Cameras

Hello Everyone, I Am Wondering How Can I Break Blocks With 2 Cameras… Well When You Press F5 It Switches To The Other Gamer, But I Get This Error

NullReferenceException: Object reference not set to an instance of an object
GeneratorRebuilt.Update () (at Assets/Scripts/GeneratorRebuilt.cs:209)

I Have Tried Alot, And I Have Been Programming In Unity Scene 3 Years Ago, But This Is Really Bugging Me, I Am Playing With Random Generation, A Destroying Blocks From The Generation Script But, Just Can’t Rape My Head Around This, Here Is The Code For The Generator

using UnityEngine;
using System.Collections;

public class Block {
    public int type;
    public bool vis;
    public GameObject block;

    public Block(int t, bool v, GameObject b) {

        type = t;
        vis = v;
        block = b;
    }
}

public class GeneratorRebuilt : MonoBehaviour {

    public static int width = 64;
    public static int depth = 64;
    public static int height = 200;
    public int heightScale = 20;
    public int heightOffset = 100;
    public float detailScale = 25.0f;
    public int sandPlacement = 110;
    public int grassPlacement = 117;
    public int dirtPlacement = 114;
    public int stonePlacement = 0;
    public string GeneratorVersion;


  
    public GameObject grassBlock;
    public GameObject dirtBlock;
    public GameObject stoneBlock;
    public GameObject sandBlock;
    public GameObject cloudBlock;
    public GameObject DiamondOreBlock;
    public GameObject BedrockBlock;
    public GameObject FirstPersonCamera;
    public GameObject ThridPersonCamera;

    Block[,,] worldBlocks = new Block[width,height,depth];


    // Use this for initialization
    void Start () {
        Debug.Log ("Loading Generator");
        if (GeneratorVersion == "") {
            Debug.LogError ("[Generator Rebuilt] Failed To Get Version... Please Input The Version Number In Your Gamemanager");
            Debug.LogError ("[Generator Rebuilt] GameObject And Place In The Version In The Generator Version");
        } else {
            Debug.LogWarning ("[Generator Rebuilt] Generator Loaded Currectly");
            Debug.Log ("[Generator Rebuilt] Generator Version: " + GeneratorVersion);
            Debug.LogWarning ("[Generator Rebuilt] Building Terrain");
            Debug.Log ("[Generator Rebuilt] Spawning Player");
            Generate ();
        }
    }
    void Generate() {
        int seed = (int)Network.time * 10;
        for(int z = 0; z < depth; z++) {
            //Setting Custom Seed
            for(int x = 0; x < width; x++) {
                int y = (int) (Mathf.PerlinNoise((x+seed)/detailScale, (z+seed)/detailScale) * heightScale) + heightOffset;
                Vector3 blockPos = new Vector3(x,y,z);
                CreateBlock (y, blockPos, true);
                while(y > 0) {
                    y--;
                    blockPos = new Vector3 (x,y,z);
                    CreateBlock (y, blockPos, false);
            }
        }
    }
        DrawClouds (3, 50);
        DigMines (2, 500);
        Debug.Log ("The Current Level Seed: "  + seed);
}

    void DrawClouds(int numClouds, int cSize) {
        for(int i = 0; i < numClouds; i++) {
            int xpos = Random.Range(0, width);
            int zpos = Random.Range (0, depth);

            for (int j = 0; j < cSize; j++) {
                Vector3 blockPos = new Vector3 (xpos, height - 1, zpos);
                GameObject newBlock = (GameObject) Instantiate (cloudBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (5, true, newBlock);
                xpos += Random.Range (-1, 2);
                zpos += Random.Range (-1, 2);
                if (xpos < 0 || xpos >= width) xpos = width / 2;
                if (zpos < 0 || zpos >= depth)zpos = depth / 2;
            }
        }
    }

    void DigMines(int numMines, int mSize) {
          
        int holeSize = 2;
        for (int i = 0; i < numMines; i++) {
            int xpos = Random.Range(10, width-10);
            int ypos = Random.Range(10, height-10);
            int zpos = Random.Range (10, depth-10);
            for (int j = 0; j < mSize; j++) {
                    for (int x = -holeSize; x <= holeSize; x++)
                  
                        for(int y = -holeSize; y <= holeSize; y++)
                      
                            for(int z = -holeSize; z <= holeSize; z++) {
                          
                                if(!(x == 0 && y == 0 && z == 0)) {
                                    Vector3 blockPos = new Vector3(xpos+x, ypos+y, zpos+z);
                                    if(worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] != null)
                                        Destroy(worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].block);
                                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;
                    }
                }

                xpos += Random.Range (-1, 2);
                ypos += Random.Range (-1, 2);
                zpos += Random.Range (-1, 2);
                if (xpos < holeSize || xpos >= width - holeSize) xpos = width / 2;
                if (ypos < holeSize || ypos >= height - holeSize) ypos = height / 2;
                if (zpos < holeSize || zpos >= depth - holeSize) zpos = depth / 2;
            }
        }
        for(int z = 1; z < depth-1; z++) {
            for (int x = 1; x < width-1; x++) {
                for (int y = 1; y < height-1; y++) {
                    if (worldBlocks [x, y, z] == null) {
                        for (int x1 = -1; x1 <= 1; x1++)
                            for (int y1 = -1; y1 <= 1; y1++)
                                for (int z1 = -1; z1 <= 1; z1++) {

                                    if (!(x1 == 0 && y1 == 0 && z1 == 0)) {
                                        Vector3 neighbour = new Vector3 (x+x1, y+y1, z+z1);
                                        DrawBlock (neighbour);
                            }
                        }
                    }
                }
            }
        }
    }

        void CreateBlock(int y, Vector3 blockPos, bool create) {

        GameObject newBlock = null;
        if (y > grassPlacement) {
            if(create)
                newBlock = (GameObject) Instantiate (grassBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (1, create, newBlock);
        } else if (y > dirtPlacement) {
                    if(create)
                            newBlock = (GameObject) Instantiate (dirtBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (2, create, newBlock);
            } else if (y > sandPlacement) {
                        if(create)
                            newBlock = (GameObject) Instantiate (sandBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (3, create, newBlock);
                } else
                    if (y > stonePlacement) {
                            if(create)
                                newBlock = (GameObject)    Instantiate (stoneBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (4, create, newBlock);
            } else if(y > 70 && y < 80 && Random.Range(0,100) < 10) {
                if(create)
                    newBlock = (GameObject)    Instantiate (DiamondOreBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (5, create, newBlock);
            } else if(y > 1 && y < 5 && Random.Range(0,100) < 60) {
                if(create)
                    newBlock = (GameObject)    Instantiate (BedrockBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (6, create, newBlock);
        }
    }

    void DrawBlock(Vector3 blockPos) {

        if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] == null) return;

        if (!worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis) {
            GameObject newBlock = null;
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = true;

            if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 1) {
                newBlock = (GameObject)Instantiate (grassBlock, blockPos, Quaternion.identity);
            } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 2) {
                newBlock = (GameObject)Instantiate (dirtBlock, blockPos, Quaternion.identity);
            } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 3) {
                newBlock = (GameObject)Instantiate (sandBlock, blockPos, Quaternion.identity);
            } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 4) {
                newBlock = (GameObject)Instantiate (stoneBlock, blockPos, Quaternion.identity);
        } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 5) {
                newBlock = (GameObject)Instantiate (DiamondOreBlock, blockPos, Quaternion.identity);
            } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 6) {

                newBlock = (GameObject)Instantiate (BedrockBlock, blockPos, Quaternion.identity);
            } else {
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = false;
            }
        }
    }
      
  
    // Update is called once per frame
    void Update() {
        if (Input.GetMouseButtonDown (0)) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2.0f, Screen.height / 2.0f, 0));
            if (Physics.Raycast (ray, out hit, 3.0f)) {
                Vector3 blockPos = hit.transform.position;

                if ((int)blockPos.y == 0)
                    return;

                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;

                Destroy (hit.transform.gameObject);

                for (int x = -1; x <= 1; x++)
                    for (int y = -1; y <= 1; y++)
                        for (int z = -1; z <= 1; z++) {

                            if (!(x == 0 && y == 0 && z == 0)) {
                                Vector3 neighbour = new Vector3 (blockPos.x + x, blockPos.y + y, blockPos.z + z);
                                DrawBlock (neighbour);
                            }
                        }
            }
        } else if (Input.GetMouseButtonDown (1)) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2.0f, Screen.height / 2.0f, 0));
            if (Physics.Raycast (ray, out hit, 3.0f)) {
                Vector3 blockPos = hit.transform.position;
                Vector3 hitVector = blockPos - hit.point;

                hitVector.x = Mathf.Abs (hitVector.x);
                hitVector.y = Mathf.Abs (hitVector.y);
                hitVector.z = Mathf.Abs (hitVector.z);

                if (hitVector.x > hitVector.z && hitVector.x > hitVector.y)
                    blockPos.x -= (int)Mathf.RoundToInt (ray.direction.x);
                else if(hitVector.y > hitVector.x && hitVector.y > hitVector.z)
                    blockPos.y -= (int)Mathf.RoundToInt (ray.direction.y);
                else
                    blockPos.z -= (int)Mathf.RoundToInt (ray.direction.z);

                CreateBlock ((int)blockPos.y, blockPos, true);

            }
        }
    }
}

And Here Is The Code For The Camera Switching-Script

using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {

    public bool IsFirstPerson = true;
    public GameObject FirstPersonCamera;
    public GameObject ThirdPersonCamera;
    public bool WaitUntillNextSwitch = false;

    // Use this for initialization
    void Start () {
        if (IsFirstPerson == true) {
            FirstPersonCamera.SetActive(true);
            ThirdPersonCamera.SetActive(false);
        } else {
            FirstPersonCamera.SetActive(false);
            ThirdPersonCamera.SetActive(true);

        }
    }

    // Update is called once per frame
    void Update () {
        if(!(WaitUntillNextSwitch == true)) {
            if(Input.GetKey(KeyCode.F5)) {
                if (IsFirstPerson == false) {
                    FirstPersonCamera.SetActive (true);
                    ThirdPersonCamera.SetActive (false);
                    IsFirstPerson = true;
                    WaitUntillNextSwitch = true;
                    StartCoroutine (WaitUntilSwitch());
                } else {
                    ThirdPersonCamera.SetActive (true);
                    FirstPersonCamera.SetActive (false);
                    IsFirstPerson = false;
                    WaitUntillNextSwitch = true;
                    StartCoroutine (WaitUntilSwitch());
                }
            }
      
        }
    }

    IEnumerator WaitUntilSwitch() {
        Debug.Log ("You Can Not Press F5");
        yield return new WaitForSeconds(2);
        WaitUntillNextSwitch = false;
        Debug.Log ("You Can Press F5");
    }
}

Also In The Generator Script I Could Use Some Help, With The Ore Generateration Its Not Really Working…

So the first problem I see here is that you are using:

 Camera.main.ScreenPointToRay

to detect user tap, however you said that you are using 2 cameras. Camera.main represents only the first active camera that unity finds with the tag “MainCamera”. You should have some kind of camera manager the finds both cameras, save them to some variables, and access them based on who is currenty visible.
Now, I am not exactly sure why do you get a null on 209, but It may have something to do that Camera.main is inactive?
You sh__l__oud check this:

I Tried To Set A Set The Cameras With

public Camera FirstPersonCamera
public Camera ThirdPersonCamera

And Setted

 Camera.main.ScreenPointToRay;

to

FirstPersonCamera.main.ScreenPointToRay
ThirdPersonCamera.main.ScreenPointToRay

Of Couse In Different Sections,

Is That What You Are Referring To?

Oh, I See What You Are Referring To Now… Yea I Fixed It, By Tagging The Two Main Cameras :slight_smile: Thanks