Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Aj_112 · Mar 07, 2016 at 10:53 AM · c#collidervariableontriggerenterpowerup

Access variable from different class

I am making a power up for my player to double up its speed if it enter's the power up prefab's Collider . There are no errors popping up but when i enter the collider of the power up prefab still there is no change in the speed . check my code and help me out to spot the error . Any help is appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour{
     public float movespeed= 12f;
     public  float turnspeed = 130f;
 
         void Update () {
         if (Input.GetKey (KeyCode.UpArrow)) {
             transform.Translate (Vector3.forward * movespeed * Time.deltaTime);
         }
         if(Input.GetKey (KeyCode.DownArrow)) {
             transform.Translate(-Vector3.forward * movespeed * Time.deltaTime);   
         }
         if(Input.GetKey (KeyCode.LeftArrow)) {
             transform.Rotate(Vector3.up * -turnspeed * Time.deltaTime);          
         }
         if(Input.GetKey (KeyCode.RightArrow)) {
             transform.Rotate(Vector3.up * turnspeed * Time.deltaTime);          
         }
 
     }
 }

Power up code

 using UnityEngine;
 using System.Collections;
 
 public class Powerup : MonoBehaviour {
 
     private Vector3 orginalscale;
     protected Collider collider;
 
     public float rotation_speed =50f;
     public float up_down_speed =0.3f;
 
     private float speedMultiplier = 2.0f;
 
     private Movement movement ;
 
     void Start () {
 
         orginalscale = transform.localScale;
         collider = GetComponent<Collider>();
 
         }
     
 
     void Update () {
         transform.Rotate(new Vector3 (0, Time.deltaTime * rotation_speed, 0));
         Vector3 position = transform.position;
         position.y -= Mathf.Sin(Time.timeSinceLevelLoad - Time.deltaTime) * up_down_speed;
         position.y += Mathf.Sin(Time.timeSinceLevelLoad) * up_down_speed;
         transform.position = position;
 
         }
         void OnTriggerEnter(Collider other) {
         collider.enabled = false;
         transform.localScale = Vector3.zero;
         GameObject thePlayer = GameObject.Find("enemytank1");
         movement=thePlayer.GetComponent< Movement>();
         movement.movespeed *= speedMultiplier;
         
         }
 
 }


  
Comment
Add comment · Show 6
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 meat5000 ♦ · Mar 07, 2016 at 10:53 AM 0
Share

Collider marked as trigger?

Player got a rigidbody?

avatar image Aj_112 meat5000 ♦ · Mar 07, 2016 at 03:02 PM 0
Share

yes i have the collider checked as trigger and the player has a rigid body.

avatar image anunitydy · Mar 07, 2016 at 03:18 PM 0
Share

why lookup player with Find rather than going through the other collider?

avatar image Aj_112 anunitydy · Mar 07, 2016 at 04:24 PM 0
Share

I am a newbie in unity, i first tried to use the other collider but it gave me errors so i used the way which i know better

avatar image Salmjak Aj_112 · Mar 07, 2016 at 05:08 PM 0
Share

@Aj_112 What errors did it give you? Are you sure the collider is even active when the player collides with it (since you disable it)?

Your current setup means: If ANYTHING collides with the object (even terrain) then it will disable itself.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sk8terboy4 · Mar 08, 2016 at 12:15 AM

@Aj_112 I assume you can call the triggers fine.

 void OnTriggerEnter(Collider other) {
              collider.enabled = false;
              transform.localScale = Vector3.zero;
             
              //GameObject thePlayer = GameObject.Find("enemytank1");
              //movement=thePlayer.GetComponent< Movement>();
             // movement.movespeed *= speedMultiplier;
              if(other.gameObject.tag == "player"){
                    other.gameObject.GetComponent<Movement>().movespeed *= speedMultiplier;
              }
    }

Just create a new tag called "player" and set that tag on the player object which has the Movement script attached to it. Once you pass through the trigger, the player speed should be doubled by speedMultiplier, keep in mind the speed is not going to decrease back to normal unless you specify otherwise. I don't know if this code will work though.

Comment
Add comment · Show 4 · 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 Aj_112 · Mar 08, 2016 at 05:43 PM 0
Share

just tried the code still the speed is not increasing :/

avatar image sk8terboy4 Aj_112 · Mar 08, 2016 at 08:09 PM 0
Share

I made a test scene and it seems to work fine. I think you need to increase the speed multiplier to say 10 in order to see a change in speed. On my capsule I had the movement script, a rigidbody, and a capsule collider. The capsule also had a tag called "player". On the Powerup object, I had the Powerup script and a box collider with Is Trigger checked. link text

avatar image Aj_112 sk8terboy4 · Mar 08, 2016 at 10:17 PM 0
Share

wow, I really appretiate your help ;) . Yes you are right the code does work . i also tried in another empty project with a cube and when it took the powerup it increased the speed . But it is't working in my main project . I read that by taking a null reference can help but i have no idea where and how to take a null reference.

Show more comments

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

C# OnTriggerEnter Issue 3 Answers

OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers

Crane Pick up script - setting new parent 0 Answers

OnTriggerEnter Issue - Collider problem 0 Answers

How to call an action from a script that is on the anonymous object you collide with? 1 Answer


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