- Home /
some scripting help
hey everybody i'm still realy new to unity and i'm trying to write a script but i'm affraid i need some help.
i had a cube where i added a particle renderer a particle emitter and a particle animator. i have a script that enables me to turn those off. but i also have a box collider on the cube that i want to turn off when the particles are off.
that's were i came up with this:
if( ParticleRenderer == false || BoxCollider == false )
only thing is that it doesn't work.
it gives me an error that says:
Assets/firesteptrough.js(2,1): BCE0043: Unexpected token: .
while there is nothing there.
so if you're willing to help me a bit with this i thank you.
Answer by Berenger · Jun 04, 2012 at 10:01 PM
Could you rephrase your second line, it's not very clear ?
Anyway, ParticleRenderer and BoxCollider are types, not boolean. You probably mean
if( !GetComponent( "ParticleRenderer" ).enabled || !collider.enabled ) ...
Yes, thank you. That's what I thought, and my code is relevant. So is hijinxbassist's answer.
Answer by hijinxbassist · Jun 04, 2012 at 10:10 PM
Is this line 2? One thing to point out right off the bat is the use of BoxCollider and ParticleRenderer. They should reference a specific instance of the class and not the class itself. Instead it would look more like.
public var myPlayer:Transform;
function Start()
{
if(myPlayer.collider.enabled==false)//Do something
}
Here i am referencing the collider on the object player where collider will refer to whatever collider is there whether its a box, sphere, or other. If you have multiple colliders you would need to get the correct collider by using GetComponent(BoxCollider). As for the 'particle renderer', you may just want to do whatever when it is done emitting?
if(!myParticleSystem.isPlaying)//Do Something
so in your context in would be
if(!myParticleSystem.isPlaying)
{
objectInQuestion.collider.enabled=false;
}
i think i dont really get it. do i put the last piece in the scipt and then thats it or do i need to write some other stuff on it?
Wow, i wrote a whole nice explanation and pressed the wrong button :( Heres a recap to my hard worked comment which none will ever see.
To answer your comment, yes it will work as long as all the holes are filled in correctly. The code below is reasonably commented, should help.
//Script on your 'cube', this is just an example
private var particleSystem:ParticleSystem;
private var boxCollider:Collider;
function Start()
{
boxCollider=transform.collider;//if this cube only has one collider
//Otherwise say
//boxCollider=transform.GetComponent(BoxCollider);
//If you are using a particle system that is attached to the 'cube'
//otherwise make a public var and drag the particle system in inspector
particleSystem=transform.GetComponent(ParticleSystem);
//Either of these lines can be used in context, w/o the declaration :)
//If you are unsure of what i mean by 'w/o declaration' :
//transform.collider.enabled=false; //turns off collider
//transform.GetComponent(ParticleSystem); //refers to prtclSystem
}
function Update() //as a working example only, not efficient
{
if(!particleSystem.isPlaying)//particle system is not emitting
{
boxCollider.enabled=false;//turns off the collider
//Same as: transform.collider.enabled=false;
//if there is only one collider on the 'cube'
}
}
I hope that helps you understand how to access single components.
Your answer
Follow this Question
Related Questions
My backpack script doubt 0 Answers
unity script error 1 Answer
How to finish my else if statement? 2 Answers
Unity Script Editor Not Working 1 Answer
Need help with multiple if statements. 3 Answers