best way to script this? - vars point to particle prefabs, not Transform

This works pretty well for the most part. It was 2 scripts, but I figured one script might be faster?

This is just a script to control collisions, player/bullet and enemy/bullets, targets.
I’m using tags for effects:
ie- non_destroyable tag should set object on fire(particles) , enemy will make object explode…

So trying to combine, and it works to a degree, but still a couple minor issues.

1- vars: using this script on all objects causes all vars to show on all objects, not just tagged ones. So I have to assign each var Transform to every prefab object (alot of work). I’d prefer to be able to have the prefab particle already listed in the script.

edit:

2- figured this out.

//vars for all collisions
var explosion : Transform;
var fire : Transform;

//will need seperate tags/functions for each enemy type : human (fire), building (fire), enemy (explosion), enemy (explosion)...



function OnTriggerEnter(hit : Collider) //onTriggerEnter | collider
{
//////////////
//non_destroyable collision here : items block bullets, have fire particles, don't die, player will crash into.
//////////
	if(hit.gameObject.tag == "non_destroyable")
	{
			//Destroy(hit.gameObject);
			var burn = Instantiate (fire, gameObject.transform.position, Quaternion.identity);
			//Destroy(gameObject); 
	}
		
	else
		{


///////////
//player collision here
////////////
		if(hit.gameObject.tag == "Player") // registers player crash (ground or building)
			{
			Destroy(hit.gameObject);
			var playerCrash = Instantiate (explosion, gameObject.transform.position, Quaternion.identity);
			//Destroy(gameObject);  //could destroy building too

//add  timer to stall instant menu at crash, maybe anim too

		Application.LoadLevel(2);   		//loads game over menu 
		UFO_HealthControl.LIVES = 3;
		UFO_HealthControl.HITS = 0;
			}
//////////////
//enemy collision here
//////////

			else
				{
					if(hit.gameObject.tag == "playerProjectile") // Register player bullets
					{
					Destroy(hit.gameObject);
					var playerKillsEnemy = Instantiate (explosion, gameObject.transform.position, Quaternion.identity);
					//Destroy(gameObject);
					}
				}
		}
	
}
  1. use a static variable if you will be using the same explosion and fire in all cases.

if it’s only different in a few cases, then use a static default_fire, default_explosion. Then when instaiating the fire or explosion, use the default_explostion if explosion is null.

I get the static variable part,

but what I don’t get is how to make the static var call the particle without using Tranform and adding it per object in editor. (Just want the script to know that )
ie:

static var explosion : Bang (name of prefab)

I coming from C# but I think it’s the same in js: since it’s static, set it on one object and it will be set on all objects.

I’m sure there is some way to load a prefab by name too, but I don’t know it off hand.

did you try
Instantiate (Bang, gameObject.transform.position, Quaternion.identity)

or
Instantiate (Resources.Load(“Bang”),gameObject.transform.position, Quaternion.identity);

Nope. Didn’t work.

Bang gave me an error, Unknown identifier.

Resource.Load gives me a ‘prefab you want to Instantiate is null’. - so that seems like it’s almost correct, but I really don’t know what the null bit means.

Thanks for the help BTW.

=========

On a positive note, I did just realize that if I place the Bang in the prefab it’ll stay there. Still, would be nice not to have to assign them all to each prefab (I have quite a few all using this script to check collision)

Solved.

I didn’t realize.

But you can select the script, scroll all the way down and add prefabs to the var on the script.
Then there’s no need to add the prefab per object containing script.