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 · Feb 13, 2012 at 03:33 AM · charactercontrollerspeedactivesetactiverecursivelypower up

Change speed of character using gameobject.active at runtime?

I have been trying to figure this out for quite some time including 2 more questions on the forums with no answers. So I'm going to go into detail on this and hopefully someone might be able to point me in the right direciton. Here is my situation:

I have a parent with 2 childed objects, which I will refer to as player1 and player2. The parent has a character controller attached to it. The game starts off with player1 active and player2 inactive. When the character controller (parent) collects a powerup and enters a trigger, player1 becomes inactive and player2 becomes active. When player 2 becomes active I want the player's speed to increase. I have the power up script attached to the Power Up game object. I am also including the movement script attached to the Parent. Please note this is not all of my script... but it should be enough to understand my problem.

Power Up Script:

private var player1 : GameObject;

private var player2: GameObject;

private var powerup: GameObject;

function Awake () {

player1 = GameObject.FindWithTag("Player");

player2 = GameObject.FindWithTag("Player2");

powerup = GameObject.FindWithTag("Powerup");

player2.SetActiveRecursively(false);

}

function OnTriggerEnter(other : Collider){

player2.SetActiveRecursively(true);

player1.SetActiveRecursively(false);

powerup.SetActiveRecursively(false);

}

Movement Script (attached to Parent Object):

var walkSpeed = 15;

var runSpeed = 40;

private var player1 : GameObject;

private var player2: GameObject;

function UpdateSmoothedMovementDirection () {

player1 = GameObject.FindWithTag("Player");

player2 = GameObject.FindWithTag("Player2");

 var h = Input.GetAxisRaw ("Horizontal");
 
 if (!canControl)
 
     h = 0.0;
 
 movement.isMoving = Mathf.Abs (h) > 0.1;
     
 if (movement.isMoving)
 
     movement.direction = Vector3 (h, 0, 0);
 
 if (controller.isGrounded) {
 
     // Smooth the speed based on the current target direction
 
     var curSmooth = movement.speedSmoothing * Time.deltaTime;
     
     // Change Character Speed
 
     var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);
 
         targetSpeed *= movement.walkSpeed;
         
     var newSpeed = Mathf.Min (Mathf.Abs(h), 1.0);
 
         newSpeed *= movement.runSpeed;
         
         if (player1.active) 
         
     
     movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);
 
     if (player2.active) 
     
     movement.speed = Mathf.Lerp (movement.speed, newSpeed, curSmooth);
     
 }

So when I run the game there is a "NullReferenceException" on the second to last line of the player movement script: "if (player2.active). i'm assuming this because the engine assumes player 2 should be active when the game starts and he is not. What i'm trying to tell the engine to do is acknowledge when player2 becomes active that the newSpeed should be used instead of the targetSpeed. If I remove this line of code the game starts and you can control player 1 at normal speed. However once I collect the power up you cannot control the the new character because I removed the line that made it possible to control him (if (player2.active). I hope someone understands what i'm trying to do here and can help me solve this. Thank you for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Berenger · Feb 13, 2012 at 04:57 AM

That's because all the Find functions only search for active objects. As only one of your players is active, you will always have an error. To fix that, you should initiate player1 and 2 from the movement script before one of them get unactivated, or use another way to know what speed should be use. Maybe get access to the movement script from the power up, and change some boolean isPlayer1 etc.

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 · Feb 13, 2012 at 05:27 AM 0
Share

wow thank you very much. Can't believe i didn't realize I was calling on an inactive object haha... problem solved! Up and running now. Thank you Berenger!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Adjust turning speed for 3rd Person Character? 1 Answer

How to add diving/rolling to FPSWalker script? (Not animation, just speed) 2 Answers

Object.operator bool evaluates oddly 1 Answer

How can I implement a SimpleMove max speed? 1 Answer

Altering Character Motor max speeds from script 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