Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GrannySmith · Aug 05, 2013 at 08:01 AM · gameobjectnull

Gameobject becomes null without a reason

Short intro: I am trying to make an Augmented Reality App, where the only interaction is that the user can scale the augmented model by pinching and play animation by just tapping on the screen. So there are several animations per model and by single touching the screen you can play one animation after the other. I thaught this should be pretty simple to handle, but i've run into a lot of problems and have tried many different things.

In my update method I am checking for Input Touches and then want to play the animation. The currentObject is statis. The interesting part is, that in the update method i can access the currentObject before the for statement in the logCurrentObject method. But then some lines later (in the Debug.Log("RENDERER...) its suddenly null. I do not understand how it can turn null, when there never is happening something with it. Note: the currentObject gets set whenever the camera found and trackable Image in the scene. This works.

 void Update ()
 {
 logCurrentObject();

     for (int i = 0; i < Input.touchCount; i++) {
         if (Input.touchCount == 1 && Input.GetTouch (i).phase == TouchPhase.Ended) {
             Debug.Log("RENDERER: "  + currentObject.name + "; ENABLED: " + currentObject.renderer.enabled);
             if (Input.GetTouch (i).position.x < Screen.width && Input.GetTouch (i).position.y < Screen.height) {
                 if (currentObject.renderer.enabled) {
                     if (counter >= currentAnimations.Length) {
                         counter = 0;
                     }
                     currentObject.animation.Play (currentAnimations [counter++]);
                 }
             }
         }
     }
 }

 public static void setCurrentObject(GameObject go, string[] animationList){
     currentObject = go;    
     currentAnimations = animationList;
     initialScale = go.transform.localScale;
     logCurrentObject();
 }
 
 private static void logCurrentObject(){
     Debug.Log("Currentobject: " + currentObject.name);
     for( int i = 0; i < currentAnimations.Length; i++){
         Debug.Log("CurrentAnimations: "+ currentAnimations[i]);
     }
 }
 void LateUpdate ()
 {
     DetectTouchMovement.Calculate ();
     if (DetectTouchMovement.pinchDistanceDelta < 0 && currentObject.transform.localScale.x > initialScale.x) {
         logCurrentObject();
         currentObject.transform.localScale = new Vector3 (currentObject.transform.localScale.x - factor, currentObject.transform.localScale.y - factor, currentObject.transform.localScale.z - factor);
         Debug.Log ("SCALE--------------: " + currentObject.transform.localScale.x + " 2: " + initialScale.x + factor * 15);
     }
     if (DetectTouchMovement.pinchDistanceDelta > 0 && this.gameObject.transform.localScale.x < initialScale.x + factor * 15) {
         logCurrentObject();
         currentObject.transform.localScale = new Vector3 (currentObject.transform.localScale.x + factor, currentObject.transform.localScale.y + factor, currentObject.transform.localScale.z + factor);
         Debug.Log ("SCALE--------------: " + currentObject.transform.localScale.x + " 2: " + initialScale.x + factor * 15);
     }
 }


Also if anyone has a recommendation how i can implement what i want to achieve, i would be really happy for every solution.

UPDATE: I was able to narrow it down a little bit. I can only access the name of the currentobject, but no component of it, like the renderer or in that case the animation. So maybe I am messing up something when setting the currentObject. As mentioned earlier I am doing this in another script which is part of the Vuforia Framework in the OnTrackingFound() method. See:

 private void OnTrackingFound ()
 {
     Renderer[] rendererComponents = GetComponentsInChildren<Renderer> (true);
     Collider[] colliderComponents = GetComponentsInChildren<Collider> (true);

     // Enable rendering:
     foreach (Renderer component in rendererComponents) {
         component.enabled = true;
         // added by me
         GameObject tmpGO = new GameObject ();
         string[] anims;
         int animCounter = 0;
         Transform[] t = gameObject.GetComponentsInChildren<Transform> ();
         trackables = GameObject.Find ("ARCamera").GetComponent<Init> ().getTrackables ();
         for (int i = 0; i < t.Length; i++) {
             for (int j = 0; j < trackables.Count; j++) {
                 if (t [i].name == trackables [j].GetContentName () + "(Clone)") {
                     tmpGO = t [i].gameObject;
                     animCounter = trackables [j].GetAnimations ().Length;
                 }
             }
         }
         anims = new string[animCounter];
         
         for (int j = 0; j < trackables.Count; j++) {
             if (tmpGO.name == trackables [j].GetContentName () + "(Clone)") {
                 anims = trackables[j].GetAnimations();
             }
         }
         
         
         AnimController.setCurrentObject (tmpGO, anims);
         
         //.GetComponentInChildren<Transform>().gameObject
     }

     // Enable colliders:
     foreach (Collider component in colliderComponents) {
         component.enabled = true;
     }

     Debug.Log ("Trackable " + mTrackableBehaviour.TrackableName + " found");
 }
Comment
Add comment · Show 4
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 KiraSensei · Aug 05, 2013 at 08:09 AM 0
Share

When does the function setCurrentObject(...) is called ? I assume this is the only place where currentObject is valuated. Is it called in the Awake or Start function ?

avatar image GrannySmith · Aug 05, 2013 at 08:18 AM 0
Share

Its called by another script which is attached to ImageTargets (the gameobject which represent real life objects which can be tracked) If the camera finds this object then the OnTrackingFound method is called (this script is part of the Vuforia framework). I just added some lines in this method where i call the setCurrentObject method from this script. This setup of the currentobject work.

avatar image KiraSensei · Aug 05, 2013 at 08:21 AM 0
Share

So I suppose it is normal behavior.

While your other script has not valuated currentObject, this Update method will play with errors.

you should check at the beginning that currentObject != null

avatar image GrannySmith · Aug 05, 2013 at 08:27 AM 0
Share

Ummm, no. When i put the line

D

ebug.Log("Currentobject is " + currentObejct.name);

inbefore the for statement in the update method, then i have the correct currentObject (as long as I am tracking one target of course). But then in the same method in the lines after the for statement, the currentObject is suddenly null. And that is what is struggling with me. Again, the problem I have is that one variable (currentObject) changes it values as it seems, during one method call (update method) though I am not accessing the variable at all.

2 Replies

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

Answer by GrannySmith · Aug 05, 2013 at 12:55 PM

I got it running now like this: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class AnimController : MonoBehaviour
 {
 
     public static int counter = 0;
     public static Vector3 initialScale;
     public static List<TrackableInfo> trackables;
     public static float factor;
     public static GameObject currentObject;
     public static List<GameObject> contentObjects;
     public static string[] currentAnimations;
     private static GameObject tmpGO;
     private static string text = "default";
     
     void Start ()
     {
     }
     
     void Update ()
     {        
         for (int i = 0; i < Input.touchCount; i++) {
             if (Input.touchCount == 1 && Input.GetTouch (i).phase == TouchPhase.Ended) {
                 Debug.Log ("TOUCHY " + currentObject.name);
                 if (Input.GetTouch (i).position.x < Screen.width && Input.GetTouch (i).position.y < Screen.height) {
 
                     if (counter >= currentAnimations.Length) {
                         counter = 0;
                     }
                     currentObject.animation.Play (currentAnimations [counter]);
                     counter++;
                 }
             }
         }
     }
     
     void LateUpdate ()
     {
         DetectTouchMovement.Calculate ();
         if(DetectTouchMovement.pinchDistanceDelta != 0){
             Debug.Log("Transform " + currentObject.transform.localScale.x + "; initial " + initialScale.x + "; factored " +initialScale.x + factor * 15);    
         }
         if (DetectTouchMovement.pinchDistanceDelta < 0 && currentObject.transform.localScale.x > initialScale.x) {
             logCurrentObject ();
             currentObject.transform.localScale = new Vector3 (currentObject.transform.localScale.x - factor, currentObject.transform.localScale.y - factor, currentObject.transform.localScale.z - factor);
             Debug.Log ("SCALE--------------: " + currentObject.transform.localScale.x + " 2: " + initialScale.x + factor * 15);
         }
         if (DetectTouchMovement.pinchDistanceDelta > 0 && currentObject.transform.localScale.x < initialScale.x + factor * 15) {
             logCurrentObject ();
             currentObject.transform.localScale = new Vector3 (currentObject.transform.localScale.x + factor, currentObject.transform.localScale.y + factor, currentObject.transform.localScale.z + factor);
             Debug.Log ("SCALE--------------: " + currentObject.transform.localScale.x + " 2: " + initialScale.x + factor * 15);
         }
     }
     
     public static void resetScale(){
         currentObject.transform.localScale = initialScale;    
     }
     
     public static void setCurrentObject (GameObject go, string[] animationList)
     {
         currentObject = GameObject.Find(go.name);    
         currentAnimations = animationList;
         initialScale = go.transform.localScale;
         logCurrentObject ();
         factor = initialScale.x / 100 * 5;
         counter = 0;
     }
     
     private static void logCurrentObject ()
     {
         Debug.Log ("Currentobject: " + currentObject.name);
         for (int i = 0; i < currentAnimations.Length; i++) {
             Debug.Log ("CurrentAnimations: " + currentAnimations [i]);
         }
     }
 }

And in the DefaultTrackableEventHandler the OnTrackingFound() method, looks like this:

 private void OnTrackingFound ()
     {
         Renderer[] rendererComponents = GetComponentsInChildren<Renderer> (true);
         Collider[] colliderComponents = GetComponentsInChildren<Collider> (true);
 
         // Enable rendering:
         foreach (Renderer component in rendererComponents) {
             component.enabled = true;
             // added by me
             GameObject tmpGO = new GameObject ();
             string[] anims;
             int animCounter = 0;
             Transform[] t = gameObject.GetComponentsInChildren<Transform> ();
             trackables = GameObject.Find ("ARCamera").GetComponent<Init> ().getTrackables ();
             for (int i = 0; i < t.Length; i++) {
                 for (int j = 0; j < trackables.Count; j++) {
                     if (t [i].name == trackables [j].GetContentName () + "(Clone)") {
                         tmpGO = t [i].gameObject;
                         animCounter = trackables [j].GetAnimations ().Length;
                     }
                 }
             }
             anims = new string[animCounter];
             
             for (int j = 0; j < trackables.Count; j++) {
                 if (tmpGO.name == trackables [j].GetContentName () + "(Clone)") {
                     anims = trackables[j].GetAnimations();
                 }
             }
             
             
             AnimController.setCurrentObject (tmpGO, anims);
             
             //.GetComponentInChildren<Transform>().gameObject
         }
 
         // Enable colliders:
         foreach (Collider component in colliderComponents) {
             component.enabled = true;
         }
 
         Debug.Log ("Trackable " + mTrackableBehaviour.TrackableName + " found");
     }

And in the OnTrackingLost() method i simply call the resetScale() method from my AnimController Script, so that whenever the trackable is lost, its initial scale is reset.

However there is one issue left, because i tried to assign the currentObject like this: public static void setCurrentObject (GameObject go, string[] animationList) { currentObject = go;
} But this does NOT work. This way i can only access the name of the currentObject and no other Components whatsoever. This is something I do not understand and wish to have clarification on this. So what does this direct assigning of one GameObject to another one actually do?

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 vfxjex · Aug 14, 2016 at 05:55 AM

If you use Vofuria All static Gameobject will be null when you load a scene. I'm not sure if this is a bug or they purposely make it like this.

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

16 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

Related Questions

How do I ask if RaycastHit returns null? 3 Answers

Accessing other GameObjects Script Variables 2 Answers

[SyncVar] GameObject Instantiate returns null. 1 Answer

Gameobject as Optional Argument 0 Answers

GameObject member becomes null for no reason 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