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 Zace · Mar 10, 2013 at 05:24 AM · destroyvariablearrays

Variable resetting back to zero

Hi,

I've been trying to step through a define array size, but for some reason everytime I destroy an object in the array, it's resetting my variable back to zero. Here's the code

 function OnMouseDown(){
             if(PlayerCCC <  GlobalVariables.ColourChangeSel.Length){
             Debug.Log("PLAYERCCC = " + PlayerCCC + " < CCS.Length = " + GlobalVariables.ColourChangeSel.Length);
                 if(GlobalVariables.ColourChangeSel[PlayerCCC] != null){
                 gameObject.renderer.material.SetColor("_Color", GlobalVariables.ColourChangeSel[PlayerCCC].renderer.material.color);
                     Destroy(GlobalVariables.ColourChangeSel[PlayerCCC]);
                     PlayerCCC += 1;
                     Debug.Log("NOT NULL. PLAYER CCC = " + PlayerCCC);
                     }
             }
     }

Can anyone tell me why this is happening or offer a solution? I've tried searching the internet and re-read the unity Script Referencing.

Thanks

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 robertbu · Mar 10, 2013 at 06:47 AM 0
Share

What variable get set to zero? PlayerCCC? If so, are there other places in the script that manipulate PlayerCCC? Where is PlayerCCC defined?

avatar image Zace · Mar 10, 2013 at 08:15 AM 0
Share

Sorry, should have been more clear. Yes, it's PlayerCCC that's being set to 0. It's a local variable that I declared at the start of the script. No other function calls or changes PlayerCCC.

avatar image Kleptomaniac · Mar 10, 2013 at 08:46 AM 0
Share

I can see nothing in here which should set your variable back to 0. Where are you declaring PlayerCCC? $$anonymous$$y best guess is that at some point during execution of your code it's rerunning your variable definition, resetting it to 0.

avatar image Zace · Mar 10, 2013 at 12:52 PM 0
Share

Hmmm. I can't see anything, here's the complete function

pragma strict

//////////////////////////////////////////////////////////////// //////////////////VARIABLE CREATION///////////////////////////// ////////////////////////////////////////////////////////////////

//Variable for random colour selection var RandomColour : float;

var PlayerCCC : int;

//////////////////////////////////////////////////////////////// //////////////////GA$$anonymous$$E START//////////////////////////////////// ////////////////////////////////////////////////////////////////

function Start () {

GlobalVariables.ColourArray[0] = Color.red; GlobalVariables.ColourArray[1] = Color.yellow; GlobalVariables.ColourArray[2] = Color.blue; GlobalVariables.ColourArray[3] = Color.green;

renderer.material.SetColor("_Color",GlobalVariables.ColourArray[Random.Range(0,GlobalVariables.ColourArray.Length)]); }

//////////////////////////////////////////////////////////////// //////////////////GA$$anonymous$$E UPDATE/////////////////////////////////// ////////////////////////////////////////////////////////////////

function Update () {

}

function On$$anonymous$$ouseDown(){ if(PlayerCCC < GlobalVariables.ColourChangeSel.Length){ Debug.Log("PLAYERCCC = " + PlayerCCC + " < CCS.Length = " + GlobalVariables.ColourChangeSel.Length); if(GlobalVariables.ColourChangeSel[PlayerCCC] != null){ gameObject.renderer.material.SetColor("_Color", GlobalVariables.ColourChangeSel[PlayerCCC].renderer.material.color); Destroy(GlobalVariables.ColourChangeSel[PlayerCCC]); PlayerCCC += 1; Debug.Log("NOT NULL. PLAYER CCC = " + PlayerCCC); } else{ PlayerCCC += 1; Debug.Log("NULL.PLAYER CCC = " + PlayerCCC); } } }`

avatar image robertbu · Mar 10, 2013 at 01:34 PM 0
Share

I don't see anything obviously wrong. If you comment out the destroy does it work? I noticed you do not initialize the variable. Is it initialized in the inspector or does some other script initialize it? Are you seeing errors in the Console? If you had an access error, PlayerCCC would not be incremented.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zace · Mar 17, 2013 at 02:40 AM

Sorry about the delay in getting back to you all. A broken computer has slowed me down for the last week. I've figured out my problem.

This script was attached to several nodes in my game and on all of the nodes it was creating a new variable for each node instead of creating a single node (PlayerCCC) for all the nodes. After changing the variable from local to global I've fixed my problem. Thanks everyone for the input and help.

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
avatar image
0

Answer by MrLucid72 · Jan 13, 2016 at 10:30 AM

![alt text][1]

Depending on your version, you may be experiencing a bug: [1]: /storage/temp/61837-array-bug.png


array-bug.png (56.8 kB)
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

13 People are following this question.

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

Related Questions

Instantiate JS error that i can't figure out 1 Answer

Bug: cannot assign public Transform variable. Unity 2D 5.2 1 Answer

Keeping a destroyed game object afterwards 0 Answers

make a variable for an array from another script ? 1 Answer

clearing an array and then re-setting it- public variables 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