Home > Net >  The object I prefabs in Unity does not work
The object I prefabs in Unity does not work

Time:12-30

I'm making an online game with Photon in unity, everything is going well, but at the last moment I faced an unexpected problem.

I have a prefab object (tank) and I throw other prefab objects into this object, but it doesn't work, but it works when I put my object (tank) in the hierarchy as prefab and throw my other objects into my object (tank) as they are not prefabs.

For example, I want to make my game a mobile platform game, so I downloaded and added a joystick from unity asset store to my game. If I assign this joystick to the movement script of my tank with the prefabs state, it does not work, but if I assign it to the movement script of my tank from the hierarchy part, it works.

What do you think is the reason for this, any ideas?

Edit, Instead of matching prefab objects from the inspector, joystick = GameObject.Find("Floating Joystick").GetComponent<FloatingJoystick>();` I matched it with my script file and it worked as I don't know why, but only the joystick in the hierarchy section worked, my fire button and camera tracking codes did not work. I think the reason is the connection I made with Photon, when connecting to photon, it is connected as server>loby>room, so the Start method works once until connecting to the room and it cannot find the specified object in the active scene. I have no idea how to solve this

sorry, my english is still bad so i use translate.

CodePudding user response:

I think what you're saying is that you're assigning other prefabs to object slots in a script attached to your tank prefab.

If you do this when you're in prefab edit mode, then all your references to other prefabs are null when you instantiate the tank.

If you instantiate the tank in Edit mode, instantiate some other prefabs, and then make your assignments, then it works.

This is my understanding of your situation, at least. If this is right, the issue you're having with editing the prefab is that other objects are also prefabs, they're not instantiated.

You should be able to fix this by dragging the other prefabs onto your tank prefab, to instantiate them in the tank prefab, then drag those instantiated prefabs into your script slots.

As with basically everything in programming there are many other ways you can solve this, too, but this sounds the most like what you're looking for.

  • Related