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.
Related
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.
The console shows me this error :
Cannot destroy GameObject that is part of a prefab instance. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
I'm confused. The Logs don't tell which prefab or GameObject is involved.
Can u help me ?
Thanks a lot for answers !
Many times when you edit prefabs you will notice that some of them can be blue, while others are with no color. The blue means that they maintain the reference to the original prefab in the project and that they are not a copy, so any change made to them wil be done to the original prefab itself, so the original prebab from which the copies (clones) are made from.
To avoid this, you need to unpack the prefabs, so that you manipulate the copy instead of the original reference.
Probably the error is due to the fact that you are destroying some prefab that is not unpacked, so not destroyable by the runtime GameObject.Destroy that is to destroy a cloned copy from the scene.
Looks like you trying to destroy part of the prefab that is not instanced to the scene. If you need to modify some prefab in editor scripts please refer to PrefabUtility but be aware that this class is accessible only from UnityEditor.
Otherwise please double check your code maybe you using the wrong reference to object after prefab instantiation.
Had this issue recently. A nested prefab was missing in one of the prefabs. Happened when an old prefab was deleted. Check your scene for "Missing Prefab"
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!
I have a prefab called square in the asset tray, I drag it into the scene to create an instance of the square prefab (in the hierarchy it is highlighted blue and has the select, revert and apply buttons in the properties). If I move/resize the instance and then press apply, the changes are applied to the prefab.
However, the square prefab has a script with a public game object. I drag the game object from the hierarchy into the slot in the instance's properties tab and is shows that the script is now referencing the actual gameobject. However, when I press apply this change isn't applied to the prefab (the prefab's script still isn't referencing any actual gameobject). All other changes to the prefab are applied.
I have tried dragging the instance into the asset tray to create a new prefab with the changes, but, as soon as I do, the script no longer references the game object.
I shouldn't have to create an entirely new prefab every time I add a public variable to a script so why can't I apply this change?
Ok you can't really drag an instance to a prefab. As a rule of thumb keep instances with instances and prefabs with prefabs.
I would instead find the instance from the prefab when this one is instantiated, with a method like findobjectoftype, or gameobject.find. Let me know if you want me to expand the answer
Solved: Prefabs can only reference other prefabs since there might not be an instance. Made the actual gameobject into an instance of a prefab and applied the prefab to the script
Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.
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 .