The object I prefabs in Unity does not work - unity3d

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.

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

Why is the NetworkIdentity component making my Game Object invisible? (Unity)

Currently trying to make a multiplayer snake game, I have never made a multiplayer game before. I am having a very strange issue where whenever I add the NetworkIdentity component to my 'Snake' game object, it becomes invisible, but is still there. The game is still functional; you just can't see the snake.
I have two pictures attached, one is the game with the NetworkIdentity component, one is the game without it. Thank you for the help.
Without component
With component
A) your image labels seem to be flipped .. exchange the Without and With ;)
and B) Afaik this is the expected behavior for any GameObject with a NetworkIdentity as long as you are not connected to any network yet. It will get enabled once you host or join a session.
You probably would rather convert this into the player prefab though and not have it in your scene from the beginning but rather let the NetworkManager automatically spawn it for every player in the session including yourself.

Child Components with rigidbody

Context
I have in my project two elements. A player (just a cube) and some eyewears.
When the glasses aren't attached to the player, i want them to have properties of a Rigidbody. But when the eyewears are attached to the player, i want them to be a static object, so the player can process the collisions and physics.
I have tried:
DestroyElement(rigidbody) when the player pickup the eyewears. When he leaves them, i recreate the rigidbody with AddComponent
It worked nice, but in the future other elements will be attached and they will not share the same properties of rigidbody. I though maybe i could save the rigidbody instance, so when the player leaves the glasses i assign it to them. I could't.
AddComponent don't accept arguments.
Then i tried to set "kinematic mode" when my player wears the eyewears. It didn't go well, my player can't jump anymore and sometimes he glitches in the floor.
How can i resolve this?
GameObject.AddComponent does take an argument, or a generic argument (preferred):
go.AddComponent<RigidBody>();
this is also possible, but deprecated, since you lose type-safety:
go.AddComponent(typeof(RigidBody));
However, RigidBody is not meant to be added/removed, and in your case I would say that kinematic mode is the way to go... but I can't tell why you're experiencing weird results with it.
Thanks to R Astra i checked again the eyewear collisions and i found out the problem. I had enabled the Convex mesh.
The col was causing trouble because the back meshes were inside the player
So, quickly i copied that eyewear and generate a new mesh. It worked!
Thank you very much!

Unity 5 // when enemy spawns , Enemy target is none?

I've got a basic AI script where I'll assign an object for the enemy to chase.
The problem is, when I turn the enemy into a prefab, the target assignment becomes blank and I cannot alter it while it's a prefab. I've tried to assign the object within the code itself but I'm not completely sure how to do so (I've tried a number of things but nothing has panned out).
Any tips on how to fix the first problem, or just how to assign a target within code would be very helpful. JavaScript would be the preferred language for code.
It's the intended behaviours of prefabs.
You can't link a gameobject which belongs to the scene, to a field of a prefab in your assets since it must be completely independant from any instance of your scene. Prefabs are intended to be instantiated, and then, you will be able to assign (through code) the public field you want to your instantiated enemy. See a prefab as a file in your HDD you can instantiate from.
Without any code it's hard to help, but I guess you can do something similar to this :
var newEnemy : EnemyAI = Instantiate(enemyPrefab);
newEnemy.target = GameObject.FindWithTag ("Player").GetComponent.<Transform>();
I'm not fluent at all with Unity script.
You are still able to "instantiate" the enemies directly in the scene by dragging and dropping the prefab into your scene, and you will be able to assign the Target of your instantiated prefab.

Unity3d saved as prefab, readding removes meshes

In Unity3d I found the feature of saving GameObjects through dragging them from the Hierarchy into the Assets Folder. I'm using this to test stuff in a different scene.
Strangely, I it doesn't work with this one properly:
after readding it, all meshes are missing:
Do you know how to fix this?
Is there a possibility to move gameObjects, maybe through code, from one scene to another one(in editor)?
It is possible to move GameObject from one scene to another by using DontDestroyOnLoad, if you need to do so in runtime.

Issue with character models in unity

I am using the Unity 4 game engine and I have two scripts, one that tells the enemy to attack the player, and one that lets the player attack the enemy. The enemy can attack the player, but the other script does not work. If i apply the script to a game object such as a capsule or cube, the script works fine, but not when i apply it to an imported character model. The script basically works by checking the enemy's health and destroying the gameobject if it is equal to or below zero. I have a feeling it has to do with the character model not being an actual game object but i am pretty sure it is. Can anyone help?
When you make the enemy character. Make an empty game object. Then create your enemy inside of that object. In the hierarchy view it should be parented by the empty object. You can also do this by dragging the enemy to the empty game object in the hierarchy view. Then attach the script to the empty game object instead of the model.
Solution
Please check all the tags you are applying to the scripts .No Script could able to work until unless the tagging is done . The tagging help the game object to differentiate between normal object and the enemy so the tagging plays a vital role to run scripts .