Calling a method from another script

Hey guys,
tried to figure this one out can’t quite get there.
should be easy enough

I have one script that switches cameras. I am trying to call a method in that script from another script when ever i want to switch the camera…

i tried to make everything static but that didnt work. at least not how i did it lol.

here is my camera change script.

using UnityEngine;
using System.Collections;

public static class CameraSwitch : MonoBehaviour {
	public GameObject Camera1;
	public GameObject Camera2;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	  public void ChangeCamera() {
	
		if (Camera1.camera.active == true)
		{
			Camera2.camera.active = true;
			Camera1.camera.active = false;
		}
		if (Camera2.camera.active == true)
		{
			Camera1.camera.active = true;
			Camera2.camera.active = false;
		}

	}
}

and when i call that script from another i do:

CameraSwitch.ChangeCamera ();

right now the error im getting is:

An object reference is required to access non-static member ‘cameraSwitch.ChangeCamera()’

i tried to make everything static but that didnt work.

so how do i fix this? not sure how to give it a ‘object reference’

thanks!

You have few a issues with your use of static here.

  1. You can’t make a monobehaviour static
  2. Static classes can only have static members (methods, variables etc.)

My first piece of advice would be to avoid static altogether until you have the hang of object references and so forth. The other option would be to make everything in the method static and take out the inheritance from monobehaviour.

Since getting object references is pretty much essential to running with unity I’ll show you how I would do it.

Change line 4 to

public class CameraSwitch : MonoBehaviour {

On your calling object use

using UnityEngine;
using System.Collections;
 
public IWantTheCameraToSwitch CameraSwitch : MonoBehaviour {
    private GameObject cameraSwitcher;

    void Start (){
        cameraSwitcher = GameObject.Find("CameraSwitcher");
    }

    void Update (){
        //Some condition that triggers the camera switcher
        cameraSwitcher.GetComponent<CameraSwitch>().ChangeCamera();

        //alternatively use this line
        cameraSwitcher.SendMessage("CameraSwitch");
    }
}

You can also store a reference to the CameraSwitch component instead of the GameObject.

you could use this if static is not helping you out

have you got the script being referenced in the code where the “CameraSwitch.ChangeCamera ();” is called? You would need something like this (taken from my own code)

public GameObject <obj>;

<obj>.transform.GetComponent< <scriptname> > ().<function> ();

so in your case it would be.

public GameObject obj; //Put the gameObject that has the CamerSwitch code on it in here
        
obj.transform.GetComponent<CameraSwitch> ().ChangeCamera ();

Here is a tutorial on the topic (note that this questions has thousands of answers already):

unitygems.com - unitygems Resources and Information.

then once you get the idea you can try this:

unitygems.com - unitygems Resources and Information.