Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stingman · Jan 21, 2012 at 05:14 AM · collisioncollidercharacter controllerpower up

How do I use a power up to change the character I'm controlling?

Here is my situation:

  1. I have a particle system with a box collider attached with "is trigger" checked.

  2. I have 2 playable characters with character controllers attached.

When you start the game you start out as "character 1" which has the tag: "Player."

When the character runs through the "powerup" which is the particle system listed above (#1) I want him to change into character #2 which has the tag: "Player 2."

I'm new to Unity3d and I could have this totally wrong... but I have the following script attached to the particle system prefab I created:

var redapple : GameObject;

var yellowapple : GameObject;

function OnTriggerEnter(other : Collider){

 if(other.gameObject.tag == "Yellow Powerup"){
    Destroy(other.gameObject); 
    redapple = GameObject.FindWithTag("Player");
    yellowapple = GameObject.FindWithTag("Player2"); 

redapple.active = false;

yellowapple.active = true;

 }

}

Any ideas guys? I've been trying to figure this out for so long now... appreciate any help. Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kilometers · Jan 21, 2012 at 05:44 AM

Since I'm primarily a C# guy, I'm not going to make a code fragment to demonstrate this, but it should send you in the right direction. Avoid using Destroy() if you can. It is costly, and you may want to return to the "redapple" object later. Instead, have two (2) GameObjects that hold redapple and yellowapple, and make them the children of a "playercontroller" GameObject. When the level loads, make sure to start things off with redapple.renderer.enabled = true, and yellowapple.renderer.enabled = false. When the playercontroller object enters the trigger, just set redapple's renderer.enabled to false, and the opposite for yellowapple.

I don't know what the apple meshes look like, but if they are the same mesh and only color is changing, you may want to consider sticking with a single "apple" GameObject and simply change the material/texture.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stingman · Jan 21, 2012 at 06:48 PM 0
Share

So I did what you said and I created a script for Gamestart to apply the renderer only to the red apple. I also changed the powerup script accordingly. However, when the game starts I'm controlling both apples haha... it's kind of funny. Their meshes are intertwined with each other. When the apple enters the "powerup" the mesh renderer of the parent apple object turns off in the inspector ( I can see it uncheck itself), but the child objects are not affected (the actual red and yellow apples). Here are my scripts... any ideas? Also their meshes are not at all the same, so the idea of just changing color won't work. Each apple has different "abilities" as well.

APPLE START:

var redapple : GameObject;

var yellowapple : GameObject;

function Start () {

redapple = GameObject.FindWithTag("Player");

yellowapple = GameObject.FindWithTag("Player2");

redapple.renderer.enabled = true;

yellowapple.renderer.enabled = false;

}

YELLOW POWERUP:

var redapple : GameObject;

var yellowapple : GameObject;

function Start () {

redapple = GameObject.FindWithTag("Player");

yellowapple = GameObject.FindWithTag("Player2");

redapple.renderer.enabled = true;

yellowapple.renderer.enabled = false;

}

Not sure if I need to declare the variables in both scripts... still new to Unity. The apple start script is attached the parent object containing both apples. The yellow powerup script is attached to the powerup with "is trigger" checked.

avatar image stingman · Jan 21, 2012 at 06:52 PM 0
Share

Sorry I copied the wrong script for the yellow powerup. Here is the correct one:

var redapple : GameObject;

var yellowapple : GameObject;

function OnTriggerEnter(CharacterController : Collider){

    redapple = GameObject.FindWithTag("Player");

    yellowapple = GameObject.FindWithTag("Player2"); 

redapple.renderer.enabled = false;

yellowapple.renderer.enabled = true;

}

avatar image
0

Answer by Azound · Jan 21, 2012 at 06:02 AM

It looks like you put a component on the Particle System looking for when it collides with the Particle System... Did you mean to put this on the character instead? Otherwise the Particle System should be looking for a collision with the character.

 var redapple : GameObject;
 var yellowapple : GameObject;
 
 function OnTriggerEnter(other : Collider){
 
    if(other.gameObject.tag == "Player"){
       Destroy(this.gameObject); 
       redapple = GameObject.FindWithTag("Player");
       yellowapple = GameObject.FindWithTag("Player2"); 

       redapple.active = false;
       yellowapple.active = true;
    }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stingman · Jan 21, 2012 at 06:50 PM 0
Share

When I did this the game stopped running and said "null reference" or something along those lines. I messed around with the other suggestion from $$anonymous$$ilometers above as well. You can see my revision. I don't know if you have any thoughts regarding this... but any help is appreciated. Thank you! :)

avatar image
0

Answer by stingman · Jan 22, 2012 at 12:52 AM

Ok, thanks to the above advice and a lot of research I finally got it working correctly! Here is the code in Java in case anyone else is looking to do this:

Create a script and attach it to the POWERUP:

private var yellowapple : GameObject;

private var redapple: GameObject;

function Awake () {

yellowapple = GameObject.FindWithTag("Player2");

redapple = GameObject.FindWithTag("Player");

yellowapple.SetActiveRecursively(false);

redapple.SetActiveRecursively(true);

}

function OnTriggerEnter(other : Collider){

yellowapple.SetActiveRecursively(true);

redapple.SetActiveRecursively(false);

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A Character Controller with another shape. 0 Answers

Character Controller giving false collisions 0 Answers

Odd character controller behavior when a shield object parented to child 0 Answers

CharacterController colliding with normal Box Colliders 1 Answer

Determinism of collision judgment of Character Controller 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges