So, i want to know how i can share a value between two game objects that have the same script. The situation is this: i have two npcs that can interact with each other, in my case, it
s the Talk behavior. So, when they come together, it trigger a OnTriggerCollider that has some conditions to meet that the two npcs can Talk to each other. What i am trying to do, it
s to add some Random Value that can be shared to each other to make the Talk Behavior more dynamically. What is the best way to do that? And how i can share the same Random value between this two npc`s?
Note: when i run this code, the first npc that process the code gets 0 in probability attribute.
OnTriggerEnter code:
protected void OnTriggerEnter(Collider other)
{
if (other.CompareTag("NPC"))
{
if (isInCombatMode)
{
return;
}
else
{
if (CanTalkTo(other.gameObject))
{
Talk(other.gameObject);
}
}
}
}
CanTalkTo code:
protected bool CanTalkTo(GameObject npcTarget)
{
if (!IsInState("Talk") && IsReadyToTalk() && IsTargetNpcReadyToTalk(npcTarget))
{
NPCStatus npcTargetStatus = npcTarget.GetComponent<NPCGeneric>().status;
chanceToTalk = GetWeightInteractionProbability(npcTargetStatus);
float npcTargetChanceToTalk = 0;
npcTargetChanceToTalk = npcTarget.GetComponent<NPCGeneric>().chanceToTalk;
float avarage = (chanceToTalk + npcTargetChanceToTalk) / 2f;
Debug.Log(status.name + " " + avarage + " PT: " + chanceToTalk + " NPC PT: " + npcTargetChanceToTalk);
if (avarage >= 50f)
{
return true;
}
}
return false;
}
GetWeightInteractionProbability code:
protected float GetWeightInteractionProbability(NPCStatus npcTargetStatus)
{
float npcTargetWeight = 0;
float npcWeight = 0;
// ...
float randomValue = 0; ;
if (HasSameClass(npcTargetStatus.npcClass))
{
randomValue = Random.Range(3f, 11f) / 10f;
}
else
{
randomValue = Random.Range(1f, 8f) / 10f;
}
return ((npcWeight + npcTargetWeight) / 2f * randomValue);
}