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 /
avatar image
0
Question by nroot86 · May 26, 2011 at 08:15 PM · c#booleanpower up

Losing power up bool value

I'm just about finished on a 2D shooter game and everything is working perfectly except for my score multiplier power up. I know the bool value is being set to true when the player picks it up but when I kill an enemy the value is false...not sure what's going on

here's my code for the power up private GameObject objPlayer; private AIscript scriptAI;

 // Use this for initialization
 void Start () {
     objPlayer = (GameObject) GameObject.FindWithTag("Player");
     scriptAI = (AIscript) objPlayer.GetComponent(typeof(AIscript));
 }
 
 void removeMe ()
 {
     Destroy(gameObject);
 }
 
 void OnTriggerEnter (Collider Other)
 {
     if (Other.gameObject.tag == "Player") //only player can trigger powerups
     {
         scriptAI.isMult = true;
         Debug.Log("multi picked up!!");
         removeMe();
     }
 }

and where the value is used in the character script


//boolean vars for power ups
    public bool isLaser;
    public bool isSpread;
    public bool isMult;
    private float powUpTime; //local timer variable for laser and spread power ups
    private float powUpTime2; //local timer variable for score multiplier power up
    private float powUpLength = 10f;

void Start () { powUpTime = powUpLength; powUpTime2 = powUpLength; isLaser = false; isSpread = false; isMult = false; }

void Update () { //check for newly acquired power up and start timer if (isLaser || isSpread) { powUpTime -= Time.deltaTime; if (powUpTime <= 0) { isLaser = false; isSpread = false; powUpTime = powUpLength; } } if (isMult) { powUpTime2 -= Time.deltaTime; if (powUpTime2 <= 0) { isMult = false; powUpTime2 = powUpLength; } } }


void removeMe () //removes character
    {
        if (thisIsPlayer == false)
        {
            Instantiate(ptrScriptVariable.parAlienDeath, transform.position, Quaternion.identity);
            GUIvarScript ptrScriptGUIvar = (GUIvarScript) objPlayerStats.GetComponent(typeof(GUIvarScript));
            ptrScriptGUIvar.enemiesKilled += 1;
            //check for multiplier power up
            if(isMult)
            {
                Debug.Log("multi!!");
                ptrScriptGUIvar.score += 20;
            } else {
                Debug.Log("no multi!!!");
                ptrScriptGUIvar.score += 10;
            }
        } else {
            Instantiate(ptrScriptVariable.parHumanDeath, transform.position, Quaternion.identity);
        }
        Destroy(gameObject);
    }
i'm not sure how I could be losing the value because the logic seems right to me but I am fairly new to unity and C# so who knows

Comment
Add comment · Show 9
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 almo · May 26, 2011 at 08:22 PM 0
Share

It would help if you edit your question to have correct code formatting.

avatar image nroot86 · May 26, 2011 at 08:23 PM 0
Share

ive tried to...it keeps co$$anonymous$$g out like that

avatar image GesterX · May 26, 2011 at 08:29 PM 0
Share

Edited so the code was formatted properly - QATO was playing uo by the looks of it. I'll take a look at the question now...

avatar image almo · May 26, 2011 at 08:30 PM 0
Share

$$anonymous$$essed up still. Some of the code is missing now...

avatar image nroot86 · May 26, 2011 at 08:34 PM 0
Share

fixed it..finally

Show more comments

1 Reply

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

Answer by almo · May 26, 2011 at 08:42 PM

When the player picks up a multiplier, it sets isMulti = true in the player's instance of AIScript. When the enemies die, they check if isMulti is true in their own instance. When they die, they need to check the instance on the player.

Comment
Add comment · Show 5 · 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 nroot86 · May 26, 2011 at 08:48 PM 0
Share

remove$$anonymous$$e() is a function in AIscript that gets called from Update if the health of either the enemy or the player is <= 0. sorry if this was unclear, I removed parts of the code to keep it less cumbersome

avatar image UniteMage · May 26, 2011 at 08:48 PM 0
Share

I was always curious how variables were called when one can attach an object to the Inspector view. So it is simply called "ScriptName" object be it a variable or another object?

Thanks CSDG

avatar image almo · May 26, 2011 at 08:54 PM 0
Share

I think I got it. Take a look at the edited answer.

avatar image nroot86 · May 26, 2011 at 09:02 PM 0
Share

got it! used a pointer to the player's instance of AIscript and it's working now. Thanks for the help, the instance stuff has been the hardest thing for me to grasp

avatar image almo · May 26, 2011 at 09:36 PM 0
Share

Excellent! If this answer helped, you should "accept" it as the correct answer. Just click the little checkmark by the thumbs.

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

2 People are following this question.

avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Array Of Bools 4 Answers

Multiple Cars not working 1 Answer

Trying to find boolean value in another script 0 Answers

C# Boolean Doesn't Change Value 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