-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!

  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 12 '23 at 06:38
  • I don't see any relationship between these two ... what is `Radish`? does it inherit from `Food`? What exact errors do you get? – derHugo May 12 '23 at 07:18
  • @derHugo omg, my bad I just realized I uploaded the wrong script for the first portion. my bad, I haven't slept in a while. It's updated now and hopefully it makes more sense! Radish is the name of the GameObject that I turned into a Prefab. When I press the right mouse button, it shoots them out, like a gun but instead of bullets, it's vegetables. the error I was getting is that the reference is not set to an instance of an object, which it isn't since it's set to a prefab. – toastyducky May 12 '23 at 10:29
  • I think there is a good chance that on the object you load from resources there is no such component called `Radish` so `radishScript` stays null why not have a normal field `[SerializeField] private Radish radishPrefab;` instead of going through Resources at all (which is discouraged as per the [Best Practices](https://learn.unity.com/tutorial/assets-resources-and-assetbundles#5c7f8528edbc2a002053b5a7) btw)... also `agent` or `Playerpos` could be null here so it is a bit hard to tell without seeing your exact error log message – derHugo May 12 '23 at 11:01
  • @derHugo would it be alright to give you my discord so I can show you some pics about the error and how I have it set up? If so, it's yasmin rice#8081 and if not, that's totally fine! I completely understand :) just let me know and I'll try to continue explaining here. Thank you so much for your help by the way, I really appreciate it! – toastyducky May 12 '23 at 13:01

1 Answers1

0

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

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84