- Home /
gameObject.Find() returns null.
Hello!
I am trying to create a smoke grenade but i cannot access the gameobject 'SmokeCap' 'SmokeCap' is a child object of SmokeGrenade, and it has a particle emitter that i want to turn on after 6 seconds but the variable GrenadeMuzzle that i created on line 6 returns null instead of returning an object.
Here is my JavaScript
#pragma strict
var GrenadeTime = 0f;
private var oneSecond = 0.001f;
var duration : int = 20;
var despawnTime : int = 60;
var GrenadeMuzzle = GameObject.Find("SmokeCap");
function Start () {
}
function Update () {
print(GrenadeMuzzle);
GrenadeTime += oneSecond + Time.deltaTime;
print("GrenadeTime: " + GrenadeTime);
if (GrenadeTime >= 6) {
particleEmitter.emit = false;
GrenadeMuzzle.particleEmitter.emit = true;
}
if (GrenadeTime > duration + 6) {
GrenadeMuzzle.particleEmitter.emit = false;
}
if (GrenadeTime > despawnTime) {
Destroy(this.gameObject);
}
}
Answer by GreatBigJerk · Apr 05, 2013 at 07:45 PM
Put your GameObject.Find function call inside of an Awake function.
Okay that works not but when i run i get the two compiler errors: Assets/Scripts/SmokeGrenade.js(18,31): BCE0019: 'particleEmitter' is not a member of 'Object'. Assets/Scripts/SmokeGrenade.js(21,31): BCE0019: 'particleEmitter' is not a member of 'Object'.
And i just can't figure out what to do.
Grenade$$anonymous$$uzzle is of type GameObject. But you are trying to access a member of the type ParticleSystem. That is why it goes wrong. Try with:
Grenade$$anonymous$$uzzle.particleEmitter.emit = true;
Your answer
Follow this Question
Related Questions
Changing Color of Particles in Javascript 1 Answer
Jetpack with other particles 1 Answer
OnControllerColliderHit is not activating script. 1 Answer
Rain Particle random start 0 Answers
How to make fire spread? 3 Answers