Im currently making a script that makes a object spawn when the player collides with a object. But when the object spawns it spawns with it’s box collider and scripts disabled? any ideas why? (the prefab has everything enabled. It’s just when it spawns it all becomes disabled).
Can you post the code where you spawn the prefab?
I know I probably should use Vector 2, but the current game I’m making is a improved version of my first attempt, and my other attempt dealt with 3D objects. I will make little fixes like that when i get it working. But I don’t think that contributes to the problem at hand.
using UnityEngine;
using System.Collections;
public class random : MonoBehaviour
{
public GameObject prefab;
void OnTriggerEnter2D (Collider2D info)
{
if (info.name == "Player")
{
SpawnCoin();
}
}
void SpawnCoin()
{
Vector3 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F), 0);
Instantiate (prefab, position, Quaternion.identity);
}
}
sidenote: having a class called “random” when there is already a class called “Random” within the engine is just crying out for misnaming problems and confusion later… try to be more descriptive with your names, “RandomCoinSpawner” or something would make things a lot clearer. Also, generally classes have capital first letters and variables are lowercase first letters, so another layer of potential confusion
you could even go with making all the hardcoded values public variables and rename the function to just “Spawn” and then you just have a “RandomSpawner” which can be used for anything…
does the coin have a script running on it? there isn’t one in the images above, what is that doing in start/awake?
The object has 3 scripts on it, the image above was supposed to show the collider being disabled on spawn not the scripts (although the scripts also get disabled). the 3 scripts do: the first script attached is another OnTriggerEnter2D script which destroys the current coin if it collides with player (but since the collider for some reason is disabled once it spawns it doesn’t work). the second script attached is the only script with a Start function attached to it. this script is basically the same as the other spawner script, but this script just spawns the coin at a random place on the map right in the beginning. (this script isnt attached directly to the object, it’s attached to the gameManager, but this script actually works properly, it seems anything that spawns right in the start works, but after that it doesn’t.) and then the third script is the one mentioned above.
[quote=“LeftyRighty, post:4, topic: 584204, username:LeftyRighty”]
sidenote: having a class called “random” when there is already a class called “Random” within the engine is just crying out for misnaming problems and confusion later… try to be more descriptive with your names, “RandomCoinSpawner” or something would make things a lot clearer. Also, generally classes have capital first letters and variables are lowercase first letters, so another layer of potential confusion
http://docs.unity3d.com/ScriptReference/Random.html
you could even go with making all the hardcoded values public variables and rename the function to just “Spawn” and then you just have a “RandomSpawner” which can be used for anything…
does the coin have a script running on it? there isn’t one in the images above, what is that doing in start/awake?
[/quote]Please also keep in mind This script works in the same exact game I’ve made previously. This game i’m making now is just a improved version. the only real difference is now I’m using 2D object rather than 3D objects.
I’m beginning to wonder if this is a unity problem.
Are there any scripts on your prefab? sounds like an initialization problem. also your prefab collider is a trigger, what does it trigger?
yes there are three scripts on my prefab. and they all get disabled as soon as it spawns. and it triggers the spawn.
you spawn a prefab that spawns the same prefab on trigger?.. ok
first, do you disable the scripts anywhere in any of those 3 scripts ?
nope, the only thing that happens is the coin that is triggered gets destroyed. and then another coin is spawned from the same prefab.
dude would definately need to see the script that does that.
Script that destroys the object:
using UnityEngine;
using System.Collections;
public class Testo : MonoBehaviour {
public static int Num;
void OnTriggerEnter2D ( Collider2D info ){
if (info.name == "Player")
{
Debug.Log ("bam");
Num = Num+1;
Score.score += +1;
FinalScore.score1 +=+1;
Destroy(gameObject);
}
}
}
script that spawns object:
using UnityEngine;
using System.Collections;
public class randomCoinSpawn: MonoBehaviour
{
public GameObject prefab;
void OnTriggerEnter2D (Collider2D info)
{
if (info.name == "Player")
{
SpawnCoin();
}
}
void SpawnCoin()
{
Vector2 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F));
Instantiate (prefab, position, Quaternion.identity);
}
}
script that spawns object in beginning (kind of irrelevant but I’m pasting all codes that have anything to do with this prefab) :
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour {
public GameObject prefab;
void Start () {
Vector3 position = new Vector3 (Random.Range (-9f, 9F), Random.Range (-5.56F, 5.27F), 0);
Instantiate (prefab, position, Quaternion.identity);
}
}
that’s every single script that deals with this object in any way
Well I can’t see anything wrong at first glance, how many debug.log “bam” are you getting? are you getting any warnings?
I’m getting a strong sense of deja vu with this thread… what version of unity are you using? did you upgrade from one version to another and have the project “upgrade” to the new version?
I’m only getting one. After that anything I run into gives me absolutely nothing
I’m currently using 5.0.2f1. And I started making this project in 5.0.2f1 but the older version of this game I made (which I’m referencing from) was made in 5.0.1f1