- Home /
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;
}
}
Could you add the script that is attached to the camera?
Here is the code that attached to camera:
using UnityEngine; using System.Collections;
public class LerpToNode : $$anonymous$$onoBehaviour
{
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 $$anonymous$$Node = 0, maxNode = 0;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
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);
}
}
// never$$anonymous$$e this method
public void CheckOFF()
{
check = 1;
}
}
The "this.gameObject" will point to the previous camera user was on when going back to any camera that the user has used already.
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 deter$$anonymous$$ates that only one can exists.
If you want to call different cameras, you need to eli$$anonymous$$ate the singleton and have the cameras references in a place.
So if I make a array of cameras with the reference inside of the editor and switch accordingly and delete the singleton, that should be good?
Answer by Martin_Gonzalez · Mar 29, 2018 at 06:54 PM
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.
Answer by abotrix · Mar 29, 2018 at 06:46 PM
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.