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 Al-Anselmo · Dec 25, 2010 at 04:06 AM · getcomponentstatic-varsmultiple-objects

Static variable problem when using same script on multiple objects!

It works great when I have just 1 gameObject in the scene, but not when I have more than one.

After pressing the mouse, a raycast will be cast and hit an object tagged "RemoveCollision". The object has a script "VerifyRelatedJS.js" which holds another gameObject, which has a "ChangeMaterial.js" script with a static variable named "remainActive". I need to set the "remainActive" variable to true of just the gameObject inside the "VerifyRelatedJS.js" script (of the object that was hit).

The problem is that when I hit something, my project is changing the remainActive variable of all the gameObjects in the scene that have the "ChangeMaterial.js" script.

So: press mouse -> hit object -> remainActive = true (of ONLY the object in VerifyRelatedJS)

This is what I have:

Shoot.js - the script used to hit (is attached to Crosshair, which is not child of Player)

var range;

function Start () { range = ChangeMaterial.powerDist + 2; }

function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;

 Debug.DrawRay (ray.origin, ray.direction * range, Color.green);
 var facingDirection = transform.TransformDirection (Vector3 (0, 0, 1));

 if (Input.GetButtonDown("Power"))
 {
     if (Physics.Raycast (ray, hit, range))
     {
         if (hit.collider.gameObject.CompareTag ("RemoveCollision"))
         {
             hit.collider.isTrigger = true;

             // Get the object related to the one hit
             var affectThis : GameObject =  hit.collider.gameObject.GetComponent(VerifyRelatedJS).relatedObject;
             // Now, change the var remainActive to true from its ChangeMaterial script
             var obj : ChangeMaterial = affectThis.GetComponent(ChangeMaterial);
             obj.remainActive = true;
         }
         else
         {
             Debug.Log ("Nothing hit yet." + " I hit: " + hit.collider.name);
         }
     }
 }

}

VerifyRelatedJS.js - script added to the gameObject that is hit and is tagged "RemoveCollision"

var relatedObject : ChangeMaterial;

ChangeMaterial.js - holds the main code/ script added to the same object as VerifyRelatedJS' relatedObject

enum feedbackType {switchMaterial, switchColor, zFighting}; var whatToUse : feedbackType = feedbackType.switchMaterial;

var mat1 : Material; var mat2 : Material; var colorStart : Color; var colorEnd : Color; var zFightingObject : GameObject;

private var dist; static var powerDist : float;

private var changeColor : boolean = false; var player : GameObject; var particleFeedback : Transform;

private var zFightIt : boolean = false; // The static variable that is accessed from other scripts: static var remainActive : boolean = false; var remAct : boolean = false;

function Start() { powerDist = 7.5; player = GameObject.FindWithTag("Player");

 if (whatToUse == feedbackType.zFighting)
 {
     zFightingObject.gameObject.active = false;
 }

}

function Update () { remAct = remainActive;

 dist = Vector3.Distance(player.transform.position, transform.position);

 if (dist <= powerDist)
 {
     // Change the Color of the interactive object
     if (changeColor == false)
     {
         changeColor = true;
         // Instantiate the feedback
         var theParticleFeedback = Instantiate (particleFeedback, transform.position, transform.rotation);
         Destroy (theParticleFeedback, 3);
     }

     /*if (whatToUse == feedbackType.switchColor)
     {
         renderer.material.color = Color.Lerp (colorStart, colorEnd, 4);
     }

     if (whatToUse == feedbackType.switchMaterial)
     {
         renderer.material = mat2;
     }*/

     if (whatToUse == feedbackType.zFighting)
     {
         if (zFightIt == false)
         {
             zFightIt = true;
             zFightingObject.gameObject.active = true;
         }

         if (remainActive)
         {
             renderer.enabled = false;
         }
     }
 }

 else
 {
     /*if (whatToUse == feedbackType.switchColor)
     {
         renderer.material.color = colorStart;
         changeColor = false;
     }

     if (whatToUse == feedbackType.switchMaterial)
     {
         renderer.material = mat1;
         changeColor = false;
     }*/

     if (whatToUse == feedbackType.zFighting)
     {
         if (remainActive)
         {
             changeColor = true;
             zFightIt = true;
             zFightingObject.gameObject.active = true;
             renderer.enabled = false;
         }
         else
         {
             changeColor = false;
             zFightIt = false;
             zFightingObject.gameObject.active = false;
         }
     }
 }

}

I spent like 2 days reading unityAnswers but I had no success. I hope after posting my own question this will be fixed.

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
2
Best Answer

Answer by Mike 3 · Dec 25, 2010 at 04:15 AM

Remove the word static, and it should work

By definition, static variables don't belong to a certain instance, it belongs to the Type itself (so all instances of the Type see the same variable)

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 Peter G · Dec 25, 2010 at 04:16 AM 0
Share

Beat me too it :).

avatar image Al-Anselmo · Dec 25, 2010 at 05:40 AM 0
Share

It didn't work. If I remove the "static" from "static var remainActive : boolean = false;" of Change$$anonymous$$aterial.js, I get this error:

InvalidCastException: Cannot cast from source type to destination type. Shoot.Update () (at Assets/Scripts/Player/Shoot.js:35)

the line 35 of Shoot is this: var affectThis : GameObject = hit.collider/.gameObject/.GetComponent(VerifyRelatedJS).relatedObject;

What is wrong with those scripts? Any logic? what? thanks

avatar image Al-Anselmo · Dec 25, 2010 at 05:49 AM 0
Share

O$$anonymous$$G! never$$anonymous$$d my previous comment, I was typing the wrong thing.

I did what you said, removing the "static" from that variable. I also did something that might have helped to fix the problem, which was changing the script VerifyRelatedJS.js "var relatedObject : Change$$anonymous$$aterial;" to "var relatedObject : GameObject;".

It worked like a charm! Thank you for explaining how static variables work and helping me solve this bug that was cursing me! And thanks for the excellent xmas gift! $$anonymous$$erry xmas everyone.

avatar image Mike 3 · Dec 25, 2010 at 06:03 AM 0
Share

You're welcome, and merry christmas ;D

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

No one has followed this question yet.

Related Questions

Accesing components on multiple objects in c#? 2 Answers

GetComponent for all enemies 1 Answer

Why is the MeshFilter being removed? 1 Answer

Unity3D Editor Glitch 0 Answers

Error in script 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