ImTobi
March 8, 2021, 10:55am
1
My Code:
public static RectTransform[] EnemyClones;
[SerializeField] private Transform cannon;
public static int EnemyCount = 0;
private float _spawnTimer = 5f;
// Start is called before the first frame update
void Start()
{
randomX = Random.Range(0,400);
randomY = Random.Range(0,800);
EnemyClones[EnemyCount] = Instantiate(enemy, new Vector2(randomX,randomY),Quaternion.identity);
EnemyClones[EnemyCount].gameObject.SetActive(true);
EnemyClones[EnemyCount].transform.SetParent(canvas);
EnemyClones[EnemyCount].gameObject.tag = "Enemy";
EnemyCount += 1;
}
// Update is called once per frame
private void Update()
{
_spawnTimer -= Time.deltaTime;
if (_spawnTimer <= 0)
{
randomX = Random.Range(0,400);
randomY = Random.Range(0,800);
EnemyClones[EnemyCount] = Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
EnemyClones[EnemyCount].gameObject.SetActive(true);
EnemyClones[EnemyCount].transform.SetParent(canvas);
EnemyClones[EnemyCount].gameObject.tag = "Enemy";
EnemyCount += 1;
_spawnTimer = 5f;
}
if (EnemyClones != null)
EnemyClones[EnemyCount].transform.position =
Vector2.MoveTowards(EnemyClones[EnemyCount].transform.position, cannon.position, 1f);
}
Add to the top of class
Public GameObject enemy
Set the enemy prefab in the scripts inspector Enemy field
Oh, and change the line to:
EnemyClones[EnemyCount] = (RectTransform)Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
ImTobi
March 8, 2021, 11:13am
3
EnemyClones[EnemyCount] = Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
NullReferenceException: Object reference not set to an instance of an object
EnemySpawner.Update () (at Assets/EnemySpawner.cs:43)
ImTobi:
EnemyClones[EnemyCount] = Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
NullReferenceException: Object reference not set to an instance of an object
EnemySpawner.Update () (at Assets/EnemySpawner.cs:43)
Dude, did you read my post?
Your reply clearly indicates you didnt do point two and three of my comment
ImTobi
March 8, 2021, 11:38am
5
timfrombriz:
Dude, did you read my post?
Your reply clearly indicates you didnt do point two and three of my comment
[FormerlySerializedAs("Enemy")] public GameObject enemy;
did that already
EnemyClones[EnemyCount] = (RectTransform)Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
did that also.
Assets\EnemySpawner.cs(43,39): error CS0030: Cannot convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.RectTransform’
and i set the Prefab of course, sry for not understanding
ImTobi
March 8, 2021, 12:35pm
6
ImTobi:
[FormerlySerializedAs("Enemy")] public GameObject enemy;
did that already
EnemyClones[EnemyCount] = (RectTransform)Instantiate(enemy, new Vector2(randomX, randomY), Quaternion.identity);
did that also.
Assets\EnemySpawner.cs(43,39): error CS0030: Cannot convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.RectTransform’
and i set the Prefab of course, sry for not understanding
and if i remove the RectTransform then it says that EnemyClones is not referenced.
The answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
also known as: Unassigned Reference Exception
also known as: Missing Reference Exception
also known as: Object reference not set to an instance of an object
http://plbm.com/?p=221
The basic steps outlined above are:
Identify what is null
Identify why it is null
Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
Let me take you step by step through it.
You see the error line, line 35?
The only possible thing that could be null in that line is audio_BGMusic
So that's part 1. That's what's null. We identified it.
Part 2... WHY is it null? Well, first we start with "who is supposed to make it not null?"
Looking up further I see line 19 sets it, with this construct:
audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
So lets take that apart. How can that fail? We have to suppose it DID fa…
Step by step, break it down, find the problem.
In this specific case, you need to allocate the EnemyClones array to something big enough to hold all the enemies you contemplate, BEFORE you start assigning to it. Otherwise it will be null.
If you don’t KNOW how big that is in advance, since arrays cannot be resized, then make it a List<RectTransform>
object instead, and when you’re done adding the instantiated enemies, you can either use the List as-is, or else do a .ToArray()
on it and get an array as you are trying to do above.
ImTobi
March 8, 2021, 4:39pm
8
Kurt-Dekker:
The answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
also known as: Unassigned Reference Exception
also known as: Missing Reference Exception
also known as: Object reference not set to an instance of an object
http://plbm.com/?p=221
The basic steps outlined above are:
Identify what is null
Identify why it is null
Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
https://discussions.unity.com/t/814091/4
Step by step, break it down, find the problem.
In this specific case, you need to allocate the EnemyClones array to something big enough to hold all the enemies you contemplate, BEFORE you start assigning to it. Otherwise it will be null.
If you don’t KNOW how big that is in advance, since arrays cannot be resized, then make it a List<RectTransform>
object instead, and when you’re done adding the instantiated enemies, you can either use the List as-is, or else do a .ToArray()
on it and get an array as you are trying to do above.
Okay thank you, i did that with an List<> but now i don’t know how to access the Health Script on the Enemy. it worked before but now when i do like other.gameObject.GetComponent<HPScript>.HP -= 15;
it just bugs out
(that is onTriggerEnter3D)
ImTobi:
it just bugs out
This is an interesting assertion. My background in entomology is limited to working at a museum over the summer one year. Could you perhaps elaborate?
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
How to understand compiler and other errors and even fix them yourself:
https://discussions.unity.com/t/824586/8
We cannot help you with scripts that “just bug out”, you will need to provide more details. Is it compiling? is it throwing a runtime exception and what exception is it throwing? Does it appear to literally be doing nothing at all?
ImTobi
March 9, 2021, 8:28am
11
it runs without an error, but i have an HP Script on the Enemys and on onTriggerEnter, i search for the Script on the Enemy and decrease its HP, but for some reason now it sometimes decreases, sometimes on the wrong enemy and sometimes it doesnt work at all, but in one playsession
Am i calling the HP Script wrong or something?
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
ImTobi
March 9, 2021, 7:29pm
13
Kurt-Dekker:
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
I will try that and reply after that, thanks