Help with items not "Respawning"
Fairly new to unity, trying to harvest resources...chopping trees, trees fall, tree local scale goes to 0,0,0 until the respawn counter is over, then it resets the position and changes local scale back to 1,1,1. The issue is that the tree does go back to its original position and rotation, and it puts all of its settings back, however, changing the localscale back to 1,1,1 doesn't work...it shows as its scale being set to 1,1,1, but there is no tree tree in that spot. Can anyone look over my code and see why its not working? Im also open to suggestions on code optimization.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemHarvestManager : MonoBehaviour
{
[Header("Assignments")]
public GameObject gameController;
public GameObject collectedItem;
private Rigidbody rigidBody;
private Vector3 originalPos;
private Quaternion originalRot;
[Header("Item Settings")]
public string itemCollider;
public string breakItemWith;
public int itemQuantityToCollect;
public bool itemRespawn;
public float secondsToGenerateCollectedItems;
public float secondsToRespawn;
[Header("Item Health")]
public float itemStartingHealth;
public float itemHealth;
public float hitCooldown;
[Header("Respawn Information")]
private readonly int speed = 90;
private readonly int respawnTime = 15;
[Header("Audio Settings")]
public AudioClip itemSound;
public AudioClip itemDestroyed;
private AudioSource audioSource;
[Header("Misc Settings")]
public bool MouseDown = false;
public bool itemDepleted = false;
public RaycastHit hit;
public float dist;
public Vector3 dir;
private void Start()
{
gameController = GameObject.Find("GameController");
itemHealth = itemStartingHealth;
originalPos = transform.position;
originalRot = transform.rotation;
rigidBody = GetComponent<Rigidbody>();
rigidBody.isKinematic = true;
audioSource = GetComponent<AudioSource>();
}
IEnumerator Harvest()
{
while (MouseDown && gameController.GetComponent<ItemInHandSelection>().itemNameInHand == breakItemWith)
{
//Last hit, initiating broken item
if (itemHealth <= 0 && itemDepleted == false)
{
itemDepleted = true;
audioSource.PlayOneShot(itemDestroyed);
rigidBody.isKinematic = false;
rigidBody.AddForce(transform.forward * speed, ForceMode.Impulse);
rigidBody.constraints = RigidbodyConstraints.None;
rigidBody.useGravity = true;
Vector3 position = new Vector3(UnityEngine.Random.Range(-1.0f, 1.0f), 0, UnityEngine.Random.Range(-1.0f, 1.0f));
yield return new WaitForSeconds(secondsToGenerateCollectedItems);
for (int i = 0; i < itemQuantityToCollect; i++)
{
Instantiate(collectedItem, transform.position + new Vector3(RandomPos(-3, 3), transform.position.y + 4, RandomPos(-3, 3)) + position, Quaternion.identity);
}
for (int i = 0; i < GetComponentsInChildren<MeshRenderer>().Length; i++)
{
GetComponentsInChildren<MeshRenderer>()[i].enabled = false;
}
{
Debug.Log("Before respawn CR");
StartCoroutine(RespawnItem1());
}
}
//Item is broken, this is to wait for breaking animation
if (itemHealth <= 0 && itemDepleted == true)
{
yield return new WaitForSeconds(secondsToGenerateCollectedItems);
}
//Item is alive and well, for now
if (itemHealth > 0)
{
audioSource.PlayOneShot(itemSound);
itemHealth--;
yield return new WaitForSeconds(hitCooldown);
}
}
}
IEnumerator RespawnItem1()
{
if (itemRespawn == true)
{
transform.localScale = new Vector3(0, 0, 0);
yield return new WaitForSeconds(respawnTime);
Debug.Log("After respawn coroutine");
rigidBody.useGravity = false;
rigidBody.isKinematic = true;
itemDepleted = false;
itemHealth = itemStartingHealth;
rigidBody.constraints = RigidbodyConstraints.FreezeAll;
transform.SetPositionAndRotation(originalPos, originalRot);
transform.localScale = new Vector3(1, 1, 1);
Debug.Log("Original Location: " + originalPos);
Debug.Log("New Location: " + transform.position);
}
else
{
DestroyImmediate(gameObject);
}
}
private float RandomPos(float LowerVal, float UpperValPlusOne)
{
return (UnityEngine.Random.Range(LowerVal, UpperValPlusOne));
}
void OnMouseDown()
{
MouseDown = true;
StartCoroutine(Harvest());
}
void OnMouseUp()
{
MouseDown = false;
}
}
Answer by Dsiak · Nov 21, 2021 at 07:42 AM
I think I might know what the problem is, do you possible have the Model on the root of your Game Object? Try creating a child called "model" with only the Mesh Filter and Mesh Renderer, reference that in your script and change the localScale of the "model" game object instead of the root of the object. Something like
[SerializeField] GameObject model //Drag and Drop the model in the inspector
model.transform.localScale = Vector3.zero;
...
model.transform.localScale = Vector3.one;
Your answer