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 unity_ew0cE1h0r8oAPQ · Sep 03, 2018 at 09:20 AM · scripting problemscriptingbasicsscriptableobject

How to share a script between multiple gameobjects but with different values

I have different objects in my scene which share the same attributes. For example canMove which shows they can be moved or not. I created a script for this. using UnityEngine;

 public class CharacterInfo : MonoBehaviour {
 
     private bool canMove = true;
     private bool matched = false;
     public bool getCanMove()
     {
         return canMove;
     }
     public void setCanMove(bool move)
     {
         canMove = move;
     }
 }

I want every object to have it's own CharacterInfo . then I have another script which will change these values based on collisions.

 using UnityEngine;
 
 public class MonkeyCollision : MonoBehaviour {
 
     public CharacterInfo character;
     private void OnCollisionEnter2D(Collision2D collision)
     {
 
         if (collision.collider.tag == "basket")
         {
             Debug.Log("Hit");
             character.setCanMove(false);
             Debug.Log(character.getCanMove().ToString());
         }
        
     }
 }

The problem that I have right now is that if canMove of an object changes then this value is changed for all other objects which have the CharacterInfo component. how can I seprate them so every gameobject has it's own values? Thanks in advance.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sonky108 · Sep 03, 2018 at 09:23 AM

Hi, the reason why it's changed across all objects is that you change static variable. Static variables are shared on all of instances of the class. Remove the static keyword and it should be fine :)

Comment
Add comment · Show 2 · 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 unity_ew0cE1h0r8oAPQ · Sep 03, 2018 at 09:35 AM 0
Share

I also tried without static keyword. But the same is happening.

avatar image Sonky108 · Sep 03, 2018 at 09:51 AM 0
Share

For standard flow you should:

  1. Remove static keyword from both booleans;

  2. Add $$anonymous$$onkeyCollision and CharacterInfo scripts to every object;

  3. Assign required reference in $$anonymous$$onkeyCollision.

Also if that's all of the code of CharacterInfo maybe it doesn't need to inherit from $$anonymous$$onoBehaviour?

avatar image
0

Answer by unity_ew0cE1h0r8oAPQ · Sep 03, 2018 at 10:13 AM

I removed static keyword. using UnityEngine;

 public class CharacterInfo : MonoBehaviour {
 
     private bool canMove = true;
     private bool matched = false;
     public bool getCanMove()
     {
         return canMove;
     }
     public void setCanMove(bool move)
     {
         canMove = move;
     }
 }

I have added MonkeyCollision and CharacterInfo scripts to every object and put references by dragging objects to character field in inspector. But still the same. Any help is appreciated. I'm struggling with this for 3 days now:D

Comment
Add comment · Show 2 · 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 Sonky108 · Sep 03, 2018 at 10:45 AM 0
Share

You are referencing one CharacterInfo in every $$anonymous$$onkeyCollision, but every $$anonymous$$onkeyCollision should have individual CharacterInfo :)

avatar image unity_ew0cE1h0r8oAPQ · Sep 03, 2018 at 11:46 AM 0
Share

Can you please guide me how to do that? If you mean dragging items to characterInfo field I already dragged each corresponding item ti that field.

avatar image
0

Answer by UNDETON · Sep 03, 2018 at 10:26 AM

The reason is that script is share to all object have it. You should use OOP inheritance in C#. https://youtu.be/ZpxNmDm-t64

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

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

Related Questions

How can I activate a GameObject Component of type script? 2 Answers

How do I make a photography function? 0 Answers

looking for a way to set an integer to another number based on a condition. 2 Answers

How to make an enemy ragdoll on death? 1 Answer

How to access and change scriptable objects on instantiated prefabs 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