Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abotrix · Mar 27, 2018 at 04:34 PM · cameragameobjectreference

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;
     }
 }

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Martin_Gonzalez · Mar 27, 2018 at 07:10 PM 0
Share

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

avatar image abotrix Martin_Gonzalez · Mar 27, 2018 at 08:03 PM 0
Share

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.

avatar image Martin_Gonzalez Martin_Gonzalez · Mar 27, 2018 at 08:06 PM 1
Share

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.

avatar image abotrix Martin_Gonzalez · Mar 27, 2018 at 08:09 PM 0
Share

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?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access main camera from different GameObject script 1 Answer

How do you reference different cameras 2 Answers

GUI controlling other game objects 0 Answers

Stumped on object not set to an instance of an object, even though it is!? 1 Answer

Can't re-enable a gameobject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges