Attaching prefab's script to another prefab as a component - unity3d

I have Player prefab with script Player
Also I have GameController prefab with script TouchMove.
This is TouchMove code:
public class TouchMove{
[SerializeField] Player player;
.....
}
So TouchMove has player object as a component, which I attach through unity inspector.
TouchMove script is responsible for player movement. That is why it neads the player object.
Also I have a lot of scenes and each one contains GameController and Player objects.
When I modify the GameController or Player components I wanted the all game objects at the all scenes to be updated. That is why I decided to create prefabs: GameController prefab and Player prefab.
In inspector I added Player prefab to GameController prefab because TouchMove of GameController requares player object.
But after it when I run the game the player script gets not active. I mean that I get unable to move player in the game. (the player script gets not working)
This problem solves if I attach Player to GameController as just game objects (not the prefabs or without prefabs)
So, my questions:
why player scripts gets not working if I attach it with prefabs and it works if I attach just as game objects (without prefabs)
Is it possible to link two prefabs in the way I described?

The reason you are getting this problem is that player prefab and specific instance of the player prefab - are not the same object. They are both GameObject-s, and can both be referenced.
In your case GameController references a player prefab, but the player in the scene is a new object, which was created on scene start by CLONING the player prefab. Since it was created only on scene start - another prefab can never reference it.
To solve this, you can take couple approaches.
First, parent both player and game controller under 1 new object, call it GameManager for example. Then you will have only one prefab - game manager, that contains both player and controller.
Second option, the one I usually use, is to find the player object at load time. Instead of passing player s a public argument, find it on awake, for example like this:
private Player player;
void Awake() {
player = FindObjectOfType<Player>();
Assert.IsNotNull(player);
}

Prefabs need to exist (be instantiated) in scenes to function, what you're attempting to do is a workflow of scriptable objects.
Instead of attaching a source prefab from your project directories through the inspector, try looking for an existing instance of the prefab in the scene (at runtime).
private void Awake()
{
player = FindObjectOfType<Player>();
}

Related

playing particle system in Unity

I am using Unity3D to develop for the HTV Vive using SteamVR. I have downloaded an asset from the asset store with explosion effect created using a particle system. I want to play the particle animation when an object is destroyed. Here is the code I am, unsuccessfully, using.
private void OnDestroy() {
explosion.GetComponent<ParticleSystem>().Play();
}
Explosion is a public variable of type GameObject set from the inspector. I drop the particle system object there.
Why is it not working? anyone a good recommendation on a short tutorial to learn to use (not to create) particle effects?
Thanks
view of the hierarchy
I have tried this with the PS as a child of the target and as an independent object.
view of the inspector (Target)
view of the inspector (particle system)
edit: for some reason, the particle effect is destroyed right after the scene starts.
Try making the explosion effect into a prefab and instantiate it when destroyed.
GameObject explosion; // Prefab asset
private void OnDestroy() {
Instantiate(explosion, transform.position, Quaternion.identity);
}
Also, don't forget the stop action to Destroy.

How to make the child of a spawned player gameobject to be private to the player using Photon Unity Networking implementing Multiplayer Game on Unity

Using the photon Unity Networking, I am spawning the player game object in a multiplayer unity game. This spawned player game objects are cloned to all devices playing that game and joined same room.
My question is that how to make a particular game object which is a child of the spawned player game object completely private to the player which spawns that child game object. Nobody else can view it.
When you Instantiate the players, instead of calling the object to instantiate on network use regular unity instantiation, just make sure that the object you are instantiating does not contain a PhotonView or anything similar.

Unity2D: Prefab not working with animator

I have a prefab that is instantiate in using my spawning script. I also have a game over panel that slide down (this is done by animation) once the player dies. The problem I'm having is that the needs to be game over panel attach to the prefab in the inspector window for the game over panel to work. This image explains everything. Please help
in Unity you can't assign scene objects to prefabs because prefabs must be usable within all your scenes, so if a scene object was assignable to a prefab then the prefab couldn't be used in another scene.

Camera rotation within a prefab

I am trying to create a multiplayer game in unity3d using a diablo like camera system. So wherever I click on the screen. The system it was working fine in singleplayer where I did not need to include a camera in a player prefab. However Now I am facing the problem where my camera rotation is also affected by the rotation of my prefab parent. The hierarchy looks like this:
There is a script added to the camera that looks like this:
using UnityEngine;
using System.Collections;
public class MainCameraComponent : MonoBehaviour {
public GameObject Reaper;
private Vector3 offset;
void Start () {
offset = transform.position - Reaper.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = Reaper.transform.position + offset;
}
}
When I run the game the camera always stays behind my character, while I want it to always stay at the same rotation. so if I order my character to walk north I would see his back, if it would walk south I wanted to see the front.
notice how the shadow changes, (thus the rotation) but i always face the back of my model. TLDR: I want my child camera to ignore the rotational change of its parent and be static. whilst letting my camera position be guided by its parent. as far as I know it seems impossible to make a networkmanager instantiate a new player prefab and attach a camera afterwards on the same hierarchy level.
Any help would be greatly appreciated.
However Now I am facing the problem where my camera rotation is also
affected by the rotation of my prefab parent
That's because your Camera is a child of the player so it will do whatever the Player/parent is doing.
Your camera should not be under the player. Remove it and make sure it is not a child of the Player or any other Object. That script should work or make the camera follow the player without making the Camera the child of the GameObject.
Remove Player = GameObject.Find ("Player"); from the LateUpdate function. You have already done this in the Start function, so it is unnecessary to do it in the LateUpdate function. GameObject.Find ("Player"); should never be called directly from the LateUpdate function. It will slow down your game.
EDIT:
After reading your comment, it looks like you want to instantiate a player with the camera. You can do this without making the camera a child of your player.
Your current Setup:
Player
Model
Projectile
Projectile Mesh
Camera
So your camera is under Player.
You new Setup:
PlayerPrefab
Player
Model
Projectile
Projectile Mesh
Camera (Attach script in question to this)
Camera is now PlayerPrefab instead of Player. Now, you can instantiate PlayerPrefab and move the Player with a script. I don't know what your move script looks like but it should be attached to the Player not PlayerPrefab. The PlayerPrefab is used to hold everything so that it can be easily instantiated as one. The code in your question should be attached to your Camera.

My Prefab disappears from script when I run the game?

I have Script attached on my Player, that script has Public GameObject variable.When I attach my Prefab to it and run game it disappears from script ?
I add my prefab to script
When I run the game it just disappears !!??
the bomb prefab that you attach must be in under your asset folder , not in the scene , I mean when you click on the bomb that you assigned it must open project tab and shows it to you if it only exist in scene , it will be disappeared
It could be that the bomb prefab you are assigning to your public GameObject variable is somehow being destroyed when you start the game.
For example:
public GameObject other;
private void Awake()
{
Destroy(other);
}
Check to see if you have any Destroy functions that could be causing the prefab to be destroyed.