Below is my code. The Sheep part of the code works perfectly fine. whenever i collide with another animal the current one stops following and the next starts to follow. but with the cow and the pig if they are following me and i collide with their pen they do not dissapear and the count of their animal does not go up, i am unable to complete the game because of this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchFollow : MonoBehaviour
{
public GameObject lastCollision;
public int sheepCount;
public GameObject SheepHouse;
public GameObject SheepHouseFinished;
public GameObject PigHouse;
public GameObject PigHouseFinished;
public int pigCount;
public GameObject CowHouse;
public GameObject CowHouseFinished;
public int cowCount;
// Start is called before the first frame update
void OnCollisionEnter(Collision other)
{
if (other.gameObject.GetComponent<SheepFollow>())
{
if (lastCollision != null)
{
lastCollision.GetComponent<SheepFollow>().enabled = false;
other.gameObject.GetComponent<SheepFollow>().enabled = true;
lastCollision = other.gameObject;
}
else
lastCollision = other.gameObject;
//You could check if you really attached MyScript here, but I think we skip that. ;-)
}
if (other.gameObject.tag == "SheepPen"&& lastCollision != null && lastCollision.gameObject.tag == "Sheep")
{
if (lastCollision != null)
{
lastCollision.GetComponent<MeshRenderer>().enabled = false;
lastCollision.GetComponent<SheepFollow>().enabled = false;
sheepCount++;
lastCollision = null;
Debug.Log(sheepCount);
if (sheepCount == 3)
{
SheepHouse.transform.gameObject.SetActive(false);
SheepHouseFinished.transform.gameObject.SetActive(true);
}
}
}
//
//
// Pigs
//
//
if (other.gameObject.GetComponent<Pigfollow>())
{
if (lastCollision != null)
{
lastCollision.GetComponent<SheepFollow>().enabled = false;
other.gameObject.GetComponent<SheepFollow>().enabled = true;
lastCollision = other.gameObject;
}
else
lastCollision = other.gameObject;
//You could check if you really attached MyScript here, but I think we skip that. ;-)
}
if (other.gameObject.tag == "PigPen" && lastCollision != null && lastCollision.gameObject.tag == "Pig")
{
if (lastCollision != null)
{
lastCollision.GetComponent<MeshRenderer>().enabled = false;
lastCollision.GetComponent<SheepFollow>().enabled = false;
pigCount++;
lastCollision = null;
Debug.Log(sheepCount);
if (pigCount == 3)
{
PigHouse.transform.gameObject.SetActive(false);
PigHouseFinished.transform.gameObject.SetActive(true);
}
}
}
//
//
// cows
//
//
if (other.gameObject.GetComponent<SheepFollow>())
{
if (lastCollision != null)
{
lastCollision.GetComponent<SheepFollow>().enabled = false;
other.gameObject.GetComponent<SheepFollow>().enabled = true;
lastCollision = other.gameObject;
}
else
lastCollision = other.gameObject;
//You could check if you really attached MyScript here, but I think we skip that. ;-)
}
if (other.gameObject.tag == "CowPen"&& lastCollision != null && lastCollision.gameObject.tag == "Cow")
{
if (lastCollision == null)
{
lastCollision.GetComponent<MeshRenderer>().enabled = false;
lastCollision.GetComponent<SheepFollow>().enabled = false;
cowCount++;
Debug.Log(cowCount);
lastCollision = null;
if (cowCount == 3)
{
CowHouse.transform.gameObject.SetActive(false);
CowHouseFinished.transform.gameObject.SetActive(true);
}
}
}
}
}