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 roux69 · Aug 05, 2011 at 03:51 AM · buildinspectorvariables

Building resets variables

Hi guys! I have a little question. I'm currently doing the Walker Boys tutorial and I've encountered a little problem.

I have cubes and spheres going around the screen. in a script common for both, I have a Boolean variable that tells if the object is a sphere (true) or a cube (false), the default is false. I then selected my spheres and set the variable to true in the inspector. But when I build the project, and try it, the variable seems to have been reset to the script default, because I see the spheres not doing what they're supposed to do.

     // Enemy Script
       // Inspector variables
       var numberOfClicks : int = 2;// Hitpoints of objects
       var waitTime : float = 2.0; // time to wait   before respawn
       var shapeColor: Color[]; // Color of Object
       var explosion :Transform; // load particle effect
       var enemyPoints : int = 1;//Value of enemy
       var isSphere : boolean  = false;
       var alive                 : boolean;
       var otherScript;
 
   // Private variables
   private var storeClicks : int;var sphere    : boolean  = false;
 
   // Start is called only once.
   function Start(){
     storeClicks = numberOfClicks;
     var startPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0);  //new random position for GameObject
     transform.position = startPosition; // relocate GameObject to new location
     RespawnWaitTime();
     otherScript = GetComponent(scriptMoveSphere);
   }
 
   // Update called every frame
   function Update () {
 
     if(numberOfClicks <= 0){
         
       if (explosion){
         Instantiate(explosion, transform.position, transform.rotation);   // Create explosion
         }
         RespawnWaitTime();
         if (isSphere){
             otherScript.initialPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0);  //new random position for GameObject
         }
         else{
             var position = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0);  //new random position for GameObject
         }
         transform.position = position; // relocate GameObject to new location
         numberOfClicks = storeClicks;
     }
   }
 
   // RespawnWaitTime is used to hide the GameObject and show it again after a set amount of time
   function RespawnWaitTime(){
 
     renderer.enabled = false;
     alive = false;
     if (isSphere)
         waitTime = Random.Range(1,3);
     yield WaitForSeconds(waitTime);
     RandomColor();
     renderer.enabled = true;
     alive = true;
 }
 
   //RandomColor is used to change the color of the GameObject
   function RandomColor(){
 
     if (shapeColor.length >0){
         var newColor = Random.Range(0,shapeColor.length);
         renderer.material.color = shapeColor[newColor];
     }
 }

I cannot use different tags because they're already use for a check in a raycast hit.

Has anybody an idea of how I could solve this problem?

Comment
Add comment · Show 3
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 roux69 · Aug 05, 2011 at 05:04 AM 0
Share

I've added the part of the script. This here is inside the function update(). isSphere is the Boolean, if true, it means it's a sphere... I guess what would help is another way to define the sphere, like a function that would find an attribute that is unique to the sphere and return true if it finds it (could we recognize the game object as the sphere? or maybe recognize the name of the gameObject?),

avatar image FTheCloud · Aug 05, 2011 at 05:30 AM 0
Share

$$anonymous$$ind of hard to help if you can't use tags. That's tough. If you post your whole script then I can know what to work with. Its hard when only seeing a sliver of it.

avatar image roux69 · Aug 05, 2011 at 05:52 AM 0
Share

alright the whole script is here, I use the the boolean isSphere 2 times in this, once in the update() function and the second is in WaitTimeRespawn() function

The script is used as a kind of spawner, but ins$$anonymous$$d of destroying the object and creating it again, it hides it and reveal it again later at another location... personally, I don't like it, I would rather have a empty gameObject serving as a spawner that this, but this is what I have to work with at the moment.

1 Reply

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

Answer by FTheCloud · Aug 05, 2011 at 09:48 AM

Ok, new answer.

Add this to your start function and it should be fine.

  function Start(){
     if(collider.GetType() == SphereCollider){
     isSphere = true
        }
     storeClicks = numberOfClicks;
     var startPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0);
     transform.position = startPosition; // relocate GameObject to new location
     RespawnWaitTime();
     otherScript = GetComponent(scriptMoveSphere);
   }

This will tell the script when it starts whether its attached to a sphere or not.

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 roux69 · Aug 05, 2011 at 05:16 PM 0
Share

It works thank you very much!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Inspector defined variable wiped away when game is built. 2 Answers

Distribute terrain in zones 3 Answers

Game works fine in editor, but build fails with 59 error in the console 2 Answers

Variables not showing up in inspector 1 Answer

The box for entering a variable's value in the inspector is not where it should be. 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