Hi! Im quite new at coding, and am also working on a simple game where the player tries to collect coins with their unicycle. However, for some reason, the coins are not instantiated even though the gameobject is attached. The error I get is this:
“There is no ‘GameObject’ attached to the “CoinGenerator” game object, but a script is trying to access it.
You probably need to add a GameObject to the game object “CoinGenerator”. Or your script needs to check if the component is attached before using it.”
Below, there is a simplified version of the code that I’m trying to make it work and two images, where one of them shows the Coin Generator (script) component before running the game, and the other one while running. Any help would be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinGenerator : MonoBehaviour
{
[SerializeField] public GameObject coin;
public void Start()
{
coin = GetComponent<GameObject>();
CoinSpawner();
}
public void Update()
{
CoinSpawner();
}
public void CoinSpawner()
{
Instantiate(coin, GeneratePos(), Quaternion.identity);
}
public Vector3 GeneratePos()
{
float x, y, z;
x = UnityEngine.Random.Range(-10f, 10f);
y = -6.5f;
z = 2f;
return new Vector3(x, y, z);
}
}
If possible, always point out the specific line of code that an error is complaining about.
I believe your problem is coin = GetComponent<GameObject>();
GameObject is not a “component”; it’s the thing that the components are attached to. If you want to get the GameObject that your script is running on, you can just write “gameObject”. As in: coin = gameObject;
However…do you really want the GameObject that the CoinGenerator script is attached to? If you do that, every time you Instantiate it, you will be making a new CoinGenerator. Probably you want to delete this line of code entirely and use the Unity inspector to set your variable to the actual coin that you want to make copies of.
In no particular order, first some Unity3D basics:
All things in the hierarchy are GameObjects.
All GameObjects have Components plugged into them.
A Transform (or RectTransform) exists on all GameObjects.
Other Components may exist on each GameObject as well.
GetComponent<>() is intended to get a Component off a GameObject, not get a GameObject.
The public GameObject reference called coin in your example may point to an in-scene object or a prefab on disk.
If it is an in-scene object, you don’t need to Instantiate it. When you press Play it will be active.
If it is a Prefab, you ned to Instantiate it.
Instantiate actually returns a NEW reference to the Instantiated thing, which you most likely will want to keep around in a variable (can reuse the same one if you can keep it straight in your head) if you want to do things with it later.
All that said, and based on your screenshots, I think you want to:
Instantiate the coin in Start()
keep a copy of that reference returned by Instantiate
The way your code is set up you are making a fresh coin EVERY FRAME (that’s how often Update() is called). That’s not likely what you want.
Finally, you can NOT use the new keyword with a Component. Instead you must have a GameObject and use its reference to do an .AddComponent<>() of your new Component, but it does not appear you would even need that in your code.
Well, what I am trying to do is, of course, instantiating some coin sprites -not coin generators- after certain conditions are met (I can handle that). Isn’t this supposed to instantiate the coins? I don’t understand what makes it instantiate something other than a Coin. How would your fix look like?
Alright I basically just removed the coin = GetComponent(); part from the Start method and now it instantiates as I want it to. Thank you all. Your answers made me understand GameObjects and Components a little bit better