DontDestroyOnLoad - how to destroy? - c#

I'm working on a Vuforia (legacy) application.
In "Play" mode in Unity I can see that some "DontDestroyOnLoad" content is being generated, (looks like it's sort of a camera).
This might be a reason of why I have some problems when switching between the scenes, so the question is how to make these elements "destroyable"?

'DontDestroyOnLoad' It means that they won't manage it automatically. so if you want to destroy it, you should destroy manually like
GameObject.Destroy(GameObject.Find("TextureBufferCamera"));

Related

My Unity Scene Was Lost but I still have the Scripts

I was following a tutorial on how to make snake. https://www.youtube.com/watch?v=N0QbmNZlxhw&list=PL1bPKmY0c-wnka3TTQhX7mfn9UoVXNP3H&index=1 When Unity crashed and I had not saved it once, however I still have the scripts for it. I tried to make it again but the map won't show up. I looked at the alpha but it was just fine. I need help finding out how to make the game again with the scripts.
You are out of luck, save your scene frequently and before testing/building. If Unity crashes your scene/inspector changes will not recover. Your scripts, however, as they are separate files retain their changes.
For avoiding this in the future I recommend you to use a Version Control System
And within the options of VCS I recommend to you Git

Getting Movement with Google Cardboard in Unity

I want to put a scene in Unity into virtual reality using Google Cardboard.
I used to be able to just drag a CardboardMain prefab into the scene, delete the main camera, use CardboardMain as the camera position, and CardboardHead to track where the user was looking.
After reading the release notes for the new updates, I thought I could drag a GVREditorEmulator and GVRControllerMain into the scene, and keep the normal camera.
Unfortunately, I can't figure out how to get the camera to follow my character with this new setup. (In this case, a rolling ball.)
If I change the position of the normal camera, it looks like it works fine in Unity, but as soon as I upload it to my phone, the user stays in the same place, while the ball rolls away. (The user can still control the ball's movements, but the camera/user doesn't follow the ball at all.)
I had thought that the chase cam demo would be useful, but that's only for Daydream, and I'm using Cardboard.
This trick seemed to work for some people. I tried in on a previous version of Unity and a previous version of the SDK and it did not seem to work. I may just need to try it on this new version, but I'm worried about going into the released code and editing it, so I'd prefer answers that don't require this.
Is there any way I can get the user to move in a Google Cardboard scene in Unity when I upload it to my iPhone?
UPDATE:
It looks as though my main camera object is not moving, making me think that something is resetting it back to the center every time, lending some more credence to the "trick" earlier. I will now try "the trick" to see if it works.
UPDATE: It doesn't look like the lines listed in the "trick" are there anymore, and the ones that are similar in the new program don't even seem to be running. Still trying to figure out what continues to reset the main camera's position.
UPDATE: Just got a response back from Google on GitHub (or at least someone working on the SDK) that says "You just need to make the node that follows the player a parent of the camera, not the same game object as the camera." I'm not exactly sure what that means, so if someone could explain that, that would most likely solve my problem. If I figure it out on my own I'll post back here.
UPDATE: Zarko Ristic posted an answer that explained what this was, but unfortunately the tracking is still off. I found out how to get Google Cardboard to work with the old SDK and posted an answer on that. Still looking for ways to get the new SDK to follow a character properly.
You can't change positioin of camera in cardboard application, position of MainCamera it always must be 0,0,0. But you can just simply made empty GameObject and make it parent of MainCamera. In Cardboard games actualy you can move parent of camera instead MainCamera directly.
Add script for tracking ball to MainCamera parent (GameObject).
This does not answer my question, but is a solution to the problem.
Do not use the newest version of the SDK. Use version 0.6
When building the app in Unity, do not select to have VR enabled under the build settings. (VR will still be enabled in the app.) (Credit: Zarko Ristic)
When putting the app onto your phone, if XCode prompts you to change any settings, you can ignore it.
In addition, disable bitcode under "Build Settings -> Enable Bitcode -> No" (Currently, this will not allow you to put your app onto the app store. I would greatly appreciate it if anyone has information on how to get it to run without doing this.)
Now your app should run on your phone correctly.

Mecanim Animation in Unity

I am facing a problem while playing a animation which is already playing at the moment.
e.g. I am currently working on boxing fighting game in which when I punch twice, first time animation is played but second time, it doesn't restart animation.
I also tried turning off that animation if it is already running, not finding any clue regarding this problem as I'm beginner in mecanim animation.
Mecanim doesn't allow you to do this directly. A state can only be in one state at a time. Either at the end and fading out or at the start and fading in, but not both at once.
You can do a hacky workaround where you make two states with the same animation and alternate between them each time you want to play that animation. It's annoying to set up and makes it take more effort if you need to refactor, but it does work.
My Animancer plugin on the Unity Asset Store has a function specifically for this situation: CrossFadeNew will always fade the animation in from the beginning. If it's already playing it will automatically create a new state to fade in while the old one fades out. Feel free to contact me via the support email address listed on the store page and in the user manual if you have any questions about it.

How to use Unity Scripting with Vuforia

This maybe a stupid question but this keeps moving in my mind.
here's the situation.
I have a small application deployed in Hololens and using vuforia extensions, I'm able to track and recognize a real 3d object. I'm able to make holograms appear too, as long as holograms are children of the target object.
What I want to do is to activate or deactivate holograms that is not a child of the target object. I can't do it through code since the script i'm manipulating is a name space.
I tried putting methods inside the namespace itself but i keep having exceptions and nothing works.
I hope you can help me with this one.
Ty
You can use Instantiate https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
to create an object as soon as Vuforia has recognizes a specific target (Wich you can check by using one of the targetablemanagers.)

Finding Main Camera in CustomUnityEditor Class

Iam writing a custom editor script in unity. I want to find the MainCamera in the scene for Custom Editor class so it can be directly assigned to the script and I dont have to assign it. I know i can find all cameras in scene and iterate over it through tags but I want to know if weather a direct method like Camera.main is available which brings the MainCamera of the scene in Custom Editor.
PS: I know it is easy to assign directly but still I want to do it by fetching through in custom editor script. Also I dont want to do it at runtime in start as I have some canvas related calculations which needs to be taken care of
OK I know this is embarrassing but here is the answer which was really simple.
You can directly use the code below to get the main camera.
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
Note:
This will find the first camera with tag like so if your scene has only one main camera then it will get you that and don't have to worry about Camera.main. But my second part of question still remains. When is the Camera.main is assigned?

Categories