Instantiate Freezes Unity Game - unity3d

I am having trouble with unity freezing every time I run my game. My game right now is very simple. I found out that my game freezes when I run this code:
void Start ()
{
for(float i = -10.5f; i < 10.5; i++){
for(float l = -10.5f; l < -0.5; l++){
Instantiate(prefab, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
}
}
}
What I am doing here is generating an array of my prefab(a grey sprite) into my scene. From my research if something is not running it is because there is something overloading or there is an infinite loop going on. Whenever I run it, in order to continue working I need to use task manager and end task. Any help is appreciated, thanks!
Update: Found out that is the instantiate if that can help any problem solvers.

I think the code code that you write has no problem at all.
I copied your code and run in empty scene and it works well.
(I assigned default Cube gameobject in variable prefab)
I guess there is some problem in script that you are going to instantiate.

sorry everyone, I think I had assigned the script to the prefab soooooo that was an issue lol.

Related

Make player die if they fall off the platform

So, iam making a 3d endless runner game with unity. I want the game to stop(just like when the player hits an obstacle) when my character falls of the platform. Keep in mind that when starting the game the character's ***y position is -1.4 ***
I tried this code but it didnt work:
if ( transform.position.y < -1.5)
{
//printing game over-stopping the game
SC_PlayerMove.instance.gameOver = true;
}
I tried your code, and it worked for me when I placed it inside the update function. If you add a Debug.Log inside the if statement, check that it prints when the condition occurs. If it does, then it might be an issue with how you're handling game over.

Why 'Is Kinematics' works different on HoloLens?

Overview
I wanted to have a cube, that I can drag around the scene with the components Collider, Rigidbody and ObjectManipulator. In play mode everything works fine, but running it on the hololens, the cube starts flying around after dragging it a few time.
Steps to reproduce (All components are set via editor, not via code)
Create new project and set it up for AR/HoloLens
Import MRTK 2.4
Create cube with a Box Collider. Set Is Trigger = true
Add Rigidbody to cube. Set Use Gravity = false and Is Kinematic = true
Add Object Manipulator to cube. I have a method getting called after On Manipulation Ended, but don't know if thats important.
Expected behavior
The rigidbody is set to Is Kinematic = true and Use Gravity = false, so that the cube stays still/stops moving after releasing dragging it. This works while trying it inside the unity play mode. But running it on the hololens, the cube behaves like Is Kinematic = false and starts flying around after interacting with it. Sometimes after the second drag and sometimes after the third time drag.
Error
Before updating this post, I didnt noticed the development console in left corner of my hololens. At the beginng of the scene I get the message [Physics.PhysX] BV4 midphase only supported on intel platforms but at that moment everything is fine. As the cube begins to fly around I get the a NullReferenceExeption: Object reference not set to an instance of an object.
I fixed my issue. I know the approximate cause, but I do not fully understand it. The method, getting called after OnManipulationEnded caused that.
I have a list, getting filled and drained by OnTriggerEnter/-Exit (exit looks the same, except add→remove):
private void OnTriggerEnter(Collider other){
if (other.gameObject.layer != 31) return;
_objectsCollidingWith.Add(other.gameObject);}
OnManipulationEnded triggered this method:
private int GetMeshes(List<KeyValuePair<Transform, Mesh>> transMeshes){
foreach (GameObject go in _objectsCollidingWith)
{
transMeshes.Add(new KeyValuePair<Transform, Mesh>(go.transform , go.GetComponent<MeshFilter>().mesh));
}
return transMeshes.Count;}
So I got alot of nullreferences from GetMeshes, because some gameobject in the list _objectsCollidingWith were null. Thats because the mesh is getting updated every once in a while. That caused a lot of nullreferences until the cube just flew away.
I used the whole time the logging provider via the device portal and couldnt see what is causing this errors. But after running the project via holographic emulation I could see in the console where they were coming from.
How did I fixed my problem?
I found this post because I realized that my OnTriggerExit didn't get called and cased having null objects and some spatial meshes with the same name triggered OnTriggerEnter very often. Also I added this line in the foreach loop in GetMeshes because once in a while there is still a null object:
if (go == null)
continue;
PS: Please forgive the strange code formatting, somehow the editor here on so does not allow me to place the brackets somewhere else

UNITY 2D - Can't change Child's position

I have created a project and put a character in it, a Ninja. I drew every part of his body and put them together into one organized gameObject. It is organized as this.
Ninja Organization
NOTE : The Left_Arm GameObject has a parent so it can be rotated by the shoulder like a real arm
Now here comes the problem. I've searched and searched for hours and haven't found a solution to something that is surely so simple and dumb. In a script, I try to move the Left_Arm parent for a little animation. Here's the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NinjaShooting : MonoBehaviour {
[Header("GameObjects")]
public Transform leftArm;
public Transform rightArm;
[Header("Other")]
public bool shooting = false;
// Update is called once per frame
void Update () {
Debug.Log(leftArm.localPosition);
leftArm.localPosition = new Vector3(100, 100, 100);
Debug.Log(leftArm.localPosition);
shooting = false;
}
}
The public variables are all assigned and everything, there are no errors in the console, but the line leftArm.localPosition = new Vector3(100, 100, 100); doesn't seem to do a thing to my character.
Here's is the Console after running it : Image
The console seems alright. The start position is (-0.3, 0.2, 0) and after leftArm.localPosition = new Vector3(100, 100, 100); the position is debugged as (100, 100, 100) as it should be. But the position changes in the inspector, nor in the game window...
Additionnally, the starter position is debugged every frame, but it shouldn't. So I assume that my position isn't taken into account or something.
I've done this so many times, and always has worked.
I even tried with a new project and recreated it, and there, mysteriously, it worked. So there is someting wrong with my organization or something. Sorry if this is a stupid question.
Thank you in advance
PS : Here is an image of the script in the editor, just in case there something wrong with that : Image
PS : The script is a it weird because I simplified it :). But this doesn't work too
This issue is likely caused by an Animator controller "locking" properties that are not actually part of its Motion. In this case, it is "locking" the Transform's position. Unity does not report any warnings or errors when you try to modify those properties, even if they are being controlled by an animation.
The typical offender is the Write Defaults property being set to true for your Animation States in your animation controller. Have a look here at the documentation for what Write Defaults does, and it will make more sense to you. I suggest changing it to false for all animation states that do not need it.

How to completely reset an ARKit scene in Unity

Hey guys I'm having a small issue with ARKit and Unity.
In the game that I'm making I'm reloading my scene when the player dies, however when the scene is reloaded all the GameObjects are still in the same position from the last session. I want all my objects to return to their starting positions when the scene is reloaded.
I saw that some variables regarding position and rotation are marked as "static" in the code. I tried changing them but I got a lot of compile errors.
Does anyone know a way around this?
Create this method and call it whenever you want to reset the scene:
using UnityEngine.XR.iOS;
.
.
.
public void ResetScene() {
ARKitWorldTrackingSessionConfiguration sessionConfig = new ARKitWorldTrackingSessionConfiguration ( UnityARAlignment.UnityARAlignmentGravity, UnityARPlaneDetection.Horizontal);
UnityARSessionNativeInterface.GetARSessionNativeInterface().RunWithConfigAndOptions(sessionConfig, UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking);
}
I found the solution!
Add these lines of code in your ARCameraManager script in the "!UnityEditor" part.
UnityARSessionRunOption options = new UnityARSessionRunOption();
options = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;
m_session.RunWithConfigAndOptions(config, options);
Now every time you reload your scene all AR elements (planes, anchors, camera tracking data) are reset.

Unity clone objects don't include scripts

Does anybody know how can I include scripts when my objects clone. In my game, I need to make that when my ball hit moving wall, then there is need to show new wall including my moving scripts. MY CASE: new wall is shown, but it is not moving.
Please help.
Kind regards
Well it is very easy.
First case: if you are using the prefab to instantiate, be sure to assign on the prefab your scripts.
Second case: if you are taking the template to instantiate directly from GameObject of wall, it should create the GameObject with exatly same scripts.
If it is still not moving, check in Inspector the cloned wall, if the scripts are enabled, and double check how the scripts work (maybe needs some initializing or whatever)
If your script is not on your prefab (for any reason), you can add via a script:
void CreateWall(){
GameObject obj = (GameObject)Instantiate(wallPrefab);
NewScript ns = obj.AddComponent<NewScript>();
}
The only advantage I could think about that way is that you can add specific components based on a specific situation. Say you want to add Script A if condition A or Script B if condition B:
void CreateWall(){
GameObject obj = (GameObject)Instantiate(wallPrefab);
switch(condition){
case A:
obj.AddComponent<ScriptA>();
break;
case B:
obj.AddComponent<ScriptB>();
break;
}
}