Reputation: 1
I'm super new to c# and I'm making a 3D game in Unity. I have an int that I want to call upon in another script, but the original script that the int is in, lives on a prefab that isn't on my terrain.
Here is my first code that lives on the Prefab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Radish : MonoBehaviour
{
[SerializeField] AudioSource munchSound;
public int foodCollisions = 0;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Cow"))
{
munchSound.Play();
foodCollisions++;
}
Destroy(gameObject);
}
}
This is my second code that lives on a GameObject on my terrain.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FriendMove : MonoBehaviour
{
public Transform Playerpos;
NavMeshAgent agent;
private GameObject radishPrefab;
private Radish radishScript;
void Start()
{
agent = GetComponent<NavMeshAgent>();
radishPrefab = Resources.Load<GameObject>("Radish");
GameObject radishInstance = Instantiate(radishPrefab);
radishScript = radishInstance.GetComponent<Radish>();
}
private void Update()
{
if (radishScript.foodCollisions == 3)
{
agent.destination = Playerpos.position - new Vector3(5, 0, 5);
}
}
}
I've tried a bunch of stuff but I keep getting errors on Unity :( please help!
Upvotes: -1
Views: 44
Reputation: 12293
You may put the Food
component on the new empty GameObject on your scene
Then add public Food food
property into your FriendMove
Drag in the Inspector (Editor) your GameObject with Food into the field in FriendMove
After that you'll be able to reach the properties of Food object from the FriendMove
Upvotes: 0