- Home /
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.
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 :)
I also tried without static keyword. But the same is happening.
For standard flow you should:
Remove static keyword from both booleans;
Add $$anonymous$$onkeyCollision and CharacterInfo scripts to every object;
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?
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
You are referencing one CharacterInfo in every $$anonymous$$onkeyCollision, but every $$anonymous$$onkeyCollision should have individual CharacterInfo :)
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.
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
Your answer
Follow this Question
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