Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why does the number of the object not change?

Discussion in 'Scripting' started by romanD16ps, May 8, 2023.

  1. romanD16ps

    romanD16ps

    Joined:
    Mar 14, 2023
    Posts:
    1
    In general, I have an online mode on the photon, and there can be 7 players maximum (in one room), they all have the same name without numbers at the end, and I need to have at the end of each car up to the 7th the corresponding. numbers (car 1 - *car name*1, car 2 - *car name*2, etc.), all cars have exactly the same names, but this script assigns a number only to the first car, and the rest are created with the same names
    this script creates a car :
    Code (CSharp):
    1. using Photon.Pun;
    2. using UnityEngine;
    3. public class SpawnManager : MonoBehaviour
    4. {
    5.     public GameObject[] Spawns;
    6.     public Transform Player;
    7.     private void Awake()
    8.     {
    9.         int carNumber = PhotonNetwork.CurrentRoom.PlayerCount;
    10.         GameObject car = PhotonNetwork.Instantiate(Player.name, Spawns[Random.Range(0, Spawns.Length)].transform.position, Quaternion.identity);
    11.         car.name += carNumber;
    12.         AllTheCarsController.AssignTagToCar(car.name, "Car" + carNumber);
    13.     }
    14. }
    and this one tries to add numbers to cars :
    Code (CSharp):
    1. using UnityEngine;
    2. public class AllTheCarsController : MonoBehaviour
    3. {
    4.     public static void AssignTagToCar(string carName, string tag)
    5.     {
    6.         // Get all the cars on the scene with tag "Car"
    7.         GameObject[] allCars = GameObject.FindGameObjectsWithTag("Car");
    8.         // Looking for the needed car by tag
    9.         foreach (GameObject car in allCars)
    10.         {
    11.             if (car.name == carName)
    12.             {
    13.                 // assigning the desired tag to the found car
    14.                 car.tag = tag;
    15.                 break;
    16.             }
    17.         }
    18.     }
    19.     public void Start()
    20.     {
    21.         // nothing needed to do
    22.     }
    23. }
    I'm really thankful if you reply me