1
\$\begingroup\$

This script is intended to produce amount of (food) objects for the players over the network. What should happen is that when a player eats one food, another food object gets created in another random place. Somehow it doesn't do that.

Here's the script.

 public class FoodManager : Photon.MonoBehaviour {

 [SerializeField] GameObject smallCirclePrefab;
 [SerializeField] float widthOfBoard = 100;
 [SerializeField] float heightOfBoard = 100;
 [SerializeField] int amtOfSmallCircle = 100;
 [SerializeField] int sizeOfCircle = 5;
 // Use this for initialization
 void Start()
 {
    if(PhotonNetwork.isMasterClient)
    produceSmallCircle(amtOfSmallCircle);
 }
 // Update is called once per frame
 void Update()
 {
 }
 void produceSmallCircle(int n)
 {
     Vector2 position;
     GameObject circle;
     float positionX, positionY;
     int ratioOfSize = 5;
     for (int i = 0; i < n; i++)
     {
         positionX = Random.Range(-widthOfBoard / 2, widthOfBoard / 2);
         positionY = Random.Range(-heightOfBoard / 2, heightOfBoard / 2);
         position = new Vector3(positionX, positionY,0);
         circle = PhotonNetwork.InstantiateSceneObject(smallCirclePrefab.name, position, transform.rotation, 0,null) as GameObject;
         circle.transform.localScale = new Vector3((float)sizeOfCircle / ratioOfSize, (float)sizeOfCircle / ratioOfSize, (float)sizeOfCircle / ratioOfSize);
     }
 }

 }

and here's the other script

 using UnityEngine; using System.Collections;

 public class FoodCollision : Photon.MonoBehaviour {

 public static float amtOfIncreaseSpeed = 1f;
 public static float amtOfIncreaseHealth = 1f;
 public static float ratioOfSpeed = 150.0f;
 public static float increaseSize = 0.001f;
 PhotonView photonView;
 BoxCollider2D boxCollider;
 SpriteRenderer sp;
 void Awake()
 {
     photonView = PhotonView.Get(this);
     boxCollider = GetComponent<BoxCollider2D>();
     sp = GetComponent<SpriteRenderer>();
 }
 // Use this for initialization
 void Start()
 {
 }
 // Update is called once per frame
 void Update()
 {
 }
 public void DestroyGO()
 {

     photonView.ownerId = PhotonNetwork.player.ID;
     boxCollider.enabled = false;
     sp.enabled = false;
     photonView.RPC("DestroyRPC", PhotonTargets.MasterClient);

 }
 [PunRPC]
 void DestroyRPC()
 {
     PhotonNetwork.Destroy(gameObject);
 }

 }
\$\endgroup\$
2
  • \$\begingroup\$ What is the Photon namespace? How does it differ from a normal MonoBehaviour? \$\endgroup\$
    – Evorlor
    Jun 28, 2016 at 12:40
  • 1
    \$\begingroup\$ it has some shortcuts for the photon unity network \$\endgroup\$ Jun 28, 2016 at 13:25

1 Answer 1

1
\$\begingroup\$

I'm not sure how your code is failing (you only say it doesn't do the right behavior, not what the actual observed behavior is), but I can see what looks like two issues. A better explanation of what is actually happening would help a lot.

One, you don't have any code to react to the collision. If the food is not being consumed when the player hovers over it, it is because you don't have any OnTriggerEnter or OnTriggerStay (or OnCollisionEnter/OnCollisionStay depending on if the collider is a trigger type or not) calls to actually cause the food consumption to happen. You need these event methods for the BoxCollider to do you any good. That's where you would call DestroyGo to remove the eaten food.

Two, it looks to me like you don't have anything that would create a new food object when the old one is removed.
Assuming that DestroyRPC/DestroyGO is called by something in a collider event as above or some other script that isn't included, you need to call produceSmallCircle on your FoodManager to generate a new one when the old one is eaten.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .