ProBuilder script standalone build exceptions
Hey folks,
Having a bit of a nightmare at the moment - my project is working as intended in the editor, but when I build & run it, one specific script seems to just not work any more.
The script in question is supposed to - when a button is pressed - briefly disable the player movement script, then modify a ProBuilder mesh (turns a straight tunnel into a twisted tunnel, like in Ocarina of Time), then enable the player movement again. Theoretically it isn't that complex, & as I say it runs perfectly in the editor.
What currently happens is that the player movement gets disabled, but then nothing else happens & movement is never enabled again. I've noticed that changing my Start() function to OnAwake() makes the button do nothing at all, which is... interesting?
If anyone has any insight at all into why this would be breaking specifically in build (I haven't built much in Unity at all, most just worked in the editor, so I'm hoping it's something easy & obvious) then I'd be exceptionally grateful.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
public class TwistyCorridor : MonoBehaviour
{
[SerializeField]
private ProBuilderMesh tunnel;
[SerializeField]
private GameObject player;
int index;
public float segments;
Vector3 input;
private Vector3 velocity = Vector3.zero;
private float smoothTime = 0.04f;
float angleToRotate;
float angleChange;
float timerOne = 0.0f;
float timerTwo = 0.0f;
bool toTwist = false;
bool twisted = false;
bool toUntwist = false;
Vertex[] vertices;
Vertex[] tempVertices;
Vertex[] startingVertices;
Vector3 tempVec = Vector3.zero;
// Start is called before the first frame update
void Start()
{
vertices = tunnel.GetVertices();
tempVertices = tunnel.GetVertices();
startingVertices = tunnel.GetVertices();
for (int i = 0; i < vertices.Length; i++)
{
index = 0;
angleToRotate = 270.0f;
angleChange = angleToRotate / segments;
for (int j = 0; j < segments; j++)
{
if (j == 1)
{
index = 2;
}
if (tempVertices[index].position.y == tempVertices[i].position.y)
{
input = tempVertices[i].position;
tempVec.x = input.x * Mathf.Cos(Mathf.Deg2Rad * angleToRotate) - input.z * Mathf.Sin(Mathf.Deg2Rad * angleToRotate);
tempVec.y = tempVertices[i].position.y;
tempVec.z = input.x * Mathf.Sin(Mathf.Deg2Rad * angleToRotate) + input.z * Mathf.Cos(Mathf.Deg2Rad * angleToRotate);
tempVertices[i].position = tempVec;
}
index += 32;
angleToRotate -= angleChange;
}
}
}
private void Update()
{
CheckInput();
if (toTwist)
{
player.GetComponent<Rigidbody>().isKinematic = true;
player.GetComponent<PlayerMovement>().enabled = false;
player.GetComponent<BoxCollider>().enabled = false;
player.GetComponent<CharacterController>().enabled = false;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].position = Vector3.SmoothDamp(vertices[i].position, tempVertices[i].position, ref velocity, smoothTime);
}
Vector3[] verts = new Vector3[vertices.Length];
tunnel.SetVertices(vertices);
for (int i = 0; i < vertices.Length; i++)
verts[i] = vertices[i].position;
tunnel.RebuildWithPositionsAndFaces(verts, tunnel.faces);
tunnel.Refresh();
tunnel.ToMesh();
tunnel.GetComponent<MeshCollider>().enabled = false;
tunnel.GetComponent<MeshCollider>().enabled = true;
timerOne += Time.deltaTime;
if(timerOne > 5.0f)
{
toTwist = false;
twisted = true;
timerOne = 0.0f;
player.GetComponent<BoxCollider>().enabled = true;
player.GetComponent<PlayerMovement>().enabled = true;
player.GetComponent<PlayerMovement>().walkOnWalls = true;
player.GetComponent<Rigidbody>().isKinematic = false;
}
}
if (toUntwist && twisted)
{
player.GetComponent<Rigidbody>().isKinematic = true;
player.GetComponent<PlayerMovement>().enabled = false;
player.GetComponent<BoxCollider>().enabled = false;
player.GetComponent<CharacterController>().enabled = false;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].position = Vector3.SmoothDamp(vertices[i].position, startingVertices[i].position, ref velocity, smoothTime);
}
Vector3[] verts = new Vector3[vertices.Length];
tunnel.SetVertices(vertices);
for (int i = 0; i < vertices.Length; i++)
verts[i] = vertices[i].position;
tunnel.RebuildWithPositionsAndFaces(verts, tunnel.faces);
tunnel.Refresh();
tunnel.ToMesh();
tunnel.GetComponent<MeshCollider>().enabled = false;
tunnel.GetComponent<MeshCollider>().enabled = true;
timerTwo += Time.deltaTime;
if (timerTwo > 5.0f)
{
toUntwist = false;
twisted = false;
timerTwo = 0.0f;
player.GetComponent<PlayerMovement>().walkOnWalls = false;
player.GetComponent<PlayerMovement>().enabled = true;
player.GetComponent<CharacterController>().enabled = true;
player.GetComponent<Rigidbody>().isKinematic = false;
}
}
}
void CheckInput()
{
if (Input.GetKeyDown("l"))
toTwist = true;
if (Input.GetKeyDown("k"))
toUntwist = true;
}
}
protip: include error message (with specific line number where error happened) in your post as well.
Your answer
Follow this Question
Related Questions
Error with Instantiate after build 0 Answers
C# suddenly no longer building on Linux 0 Answers
Adding additional C# scripts after build (Android, iOS) 1 Answer
Unity won't build my scripts 0 Answers