Obect1 to Object2 to Object1 again. Object1 points to Object2. Same Script for both objects that uses gameObject. Why?[Solved]

I have a camera switching issue with this problem where camera 1 and camera 2 are using the same script and the script is using this.gameObject. Every time I switch to camera 2 and back to camera 1, I can see in the log that this.gameObject.name is “camera 2” when camera 2 is deactivated. Any advice? Thanks.

using UnityEngine; using System.Collections;

public class LerpToNode : MonoBehaviour 
{
    public GameObject[] cam1way;
    [HideInInspector] public int currentNode = 0, whichWay = 1;
    public static LerpToNode Instance;
    public float camSpeed = 2.1f;
    public GameObject lookAtTarget;
    [HideInInspector] public int check = 1;
    public int minNode = 0, maxNode = 0;

    void Awake()
    {
        Instance = this;
    }
    
    void LateUpdate ()
    {
        LerpToPos();
    }
     
    public void LerpToPos() 
    {
        if(whichWay == 0)
        {
            Debug.Log("cam name = " + this.gameObject.name);
            
            this.gameObject.transform.position = 
                Vector3.Slerp(this.gameObject.transform.position, 
                cam1way[currentNode].transform.position,
                camSpeed*Time.deltaTime);
        }    
        else if(whichWay == 1)
        {
            Debug.Log("cam name = " + this.gameObject.name);
            this.gameObject.transform.position = 
                Vector3.Slerp(this.gameObject.transform.position, 
                cam1way[currentNode].transform.position,
                camSpeed*Time.deltaTime);
        }    
    }
     
    public void CheckOFF()
    {
        check = 1;
    }
}

Hey @abotrix ! I’m gad it works!

I leave here the answer if you want to set it as correct!

.

It’s a Singleton. If you add this to two objects and you call LerpToNode.Instance then you will be calling only one of them because the Instance variable is Static.
Singleton is a patter that determinates that only one can exists.
If you want to call different cameras, you need to eliminate the singleton and have the cameras references in a place.

Could you add the script that is attached to the camera?

Change my code around and deleting the singleton worked very nicely!

Thank a bunch for @MGDevelopmentsSt help. It was the Singleton that was using a static variable to access anywhere. I totally forgot that it was static because I was using it like candy all over my project. I just got to be mindful on how I use a singleton. I guess a fps game for a player scripts would be good use case.