I have scenes where the player instantiates lots of prefabs and I need them to persist. Can I use EditorSceneManager.SaveScene to do this? Or is this completely wrong?
If not, is there something similar? I need a way to save instantiated prefabs in its scene.
Can I use EditorSceneManager.SaveScene to do this?
No
If not, is there something similar?
No
There is no built-in function to save instantiated objects during gameplay. You have to implement this yourself as the code to do this will be big and a little bit complicated.
You need to write an Editor extension plugin for this. When Object is instantiated, save that GameObject into a List with it's information such as the type of the object, position, rotation, color and components that are attached to that GameObject.
When stop is pressed, use the Editor mode and that List to re-instantiate those GameObjects back by looping over the List.
Detect Scene Play and Stop:
void Start()
{
EditorApplication.playmodeStateChanged = OnPlayModeEnter;
}
void OnPlayModeEnter()
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
Debug.Log("Playing!");
}else{
Debug.Log("Stopped Playing!");
}
}
You do the rest!
Still not possible in 2018.2.
My work around is to save a prefab using PrefabUtility.CreatePrefab().
Related
I want to have some prefabs already in the scene and then assign the players each one to control. The Input Manager is pretty clear for assigning prefabs when a player joins, but can this be changed later?
I know I can have a parent game object with several inactive prefabs as children and activate as needed, but this won't work with my current situation. Really need to just switch altogether if possible.
I'd like to use the "join manually" option which requires me to drag prefabs into the scene in the editor, then how do I assign players to them once the scene starts?
Something like:
if (GetComponent<PlayerInput>().playerIndex == 0)
{
"the player becomes prefab 1"
}
if (GetComponent<PlayerInput>().playerIndex == 1)
{
"the player becomes prefab 2"
}
I'm not sure how to access this by code (I'm pretty new) The input manager script appears uneditable, but before I realized this I tried adding some additional public game objects.
If i understand correctly your intend is to keep the players on scene while changing who controls them, right?
You should be able to get the player disconnected an reconnected to a new index without deleting the Player object from scene. Make shure you use methods properly using the docs: Player Input Manager Docs.
Otherwise I think you need an extra layer or some twist. Maybe keep the players on their indexes but change whos controlling the Player GameObjects?
By the way you can instantiate Prefabs by code using Instantiate()
You can also use inheritance to extend PlayerInputManager and maybe add some SwitchPlayer() function if really needed.
public class CustomPIM : PlayerInputManager {}
The custom class will inherit all methods from PlayerInputManager plus whatever you add.
i am trying to develop a melee combat game i am using edge collider and below code
i noticed that when i move or enable and disable the collider from the inspector everything goes but when i stand in my palace and use AttackCol.enabled = !AttackCol.enabled;
i cannot hit and the trigger function do not called
the only difference i see is the collider color when i add it from the inspector or while i am moving its color is normal but when i enable and disable it by code its color goes pale and do not do anything
public virtual void OnTriggerEnter2D(Collider2D collision)
{
if (DamageSources.Contains( collision.tag ))
{
StartCoroutine(TakeDamage());
}
}
Make sure that your Trigger Game Object is not marked as Static.
Remove virtual from function definition.
Create another Game Object for Trigger Component and try to change activation of whole the Game Object, not collider component
Stuff like AttackCol.enabled = !AttackCol.enabled; is clever, but it can go wrong when that one is called (accidentally) more than once. I suggest trying it out in the simplest form AttackCol.enabled = true; to make sure the error is not there. Later you can still make it more elegant again! :)
i added AttackCol.isTrigger = AttackCol.enabled;
after AttackCol.enabled = !AttackCol.enabled;
and everything geos ok right now
but i think it is a work around
but i need to know why the color goes pale ?
I have a game where I need to save a scene. I'm doing this by setting the scene to a variable which I can access later. The player then goes to a main menu when they are done.
The player now wants to go back to that scene, however, when it loads normally, it resets to its original state. Is there any way to use this variable to set the scene's contents to the variable I made earlier?
This is how I made the variable: Builder1 = SceneManager.GetActiveScene();
Any help would be greatly appreciated (and yes, I do realize that the variable I made probably doesn't store the scene the way I want it to). Thanks!
Scene is just a reference to the Scene object, and not all of the GameObject's locations/information at that moment in time. If the information in a scene shouldn't be lost yet, then it is advisable to not close the scene yet. Instead, take a look at Additive Scene Loading. You can addivitely load your new scene, switch your main camera to the new scene's camera, and when you are done there you can close the added scene and return the camera to the original scene.
If you're looking to persist data between game loads, then you'll need to consider serializing all of the relevant data in your scene and build a data class to house all of it.
I am trying to create a tongue like weapon that fires from the player and then retracts back bringing back any enemies it touches. I was looking at linecast or using a mesh.
Any advice for what my work best?
The game is 3d with the player stationary in the scene and enemies flying around him. The idea was to use touch/mouse position to decide where the tongue would fire too but the 3d to 2d is causing all sorts of issues.
Any advice of where to start greatly received, I think trying two or three different approaches has left me a little bit muddled. Inspiration needed!
Here's what you'll want to do, or something to help you get started.
First you have to know exactly what you're doing to detect contact. This can be used with a trigger event with some sort of trigger box of the object.
void OnTriggerEnter (Collider object)
{
// The collision code here. Just keep reading,
}
Now it comes down to bringing the object back. You can easily just use object.gameObject.transform.position and set a new position. However this will cause the object to "jump" because you're modifying the coordinates of the object directly. So if you don't care to much about that..here's something in full you could do. This can hopefully get you started.
void OnTriggerEnter (Collider object)
{
if(object.tag == "enemy")
{
object.transform.position = new Vector3(0,0,0);
}
}
This is an example of something you could do.
Side note: You may need to change object. to object.gameObject..
I hope I've helped you enough to get you started. :)
I have a script called DialogueController, which is attached to a canvas. I would like to instantiate that canvas from the DialogueController script. Whenever I attempt to do this, all of the public objects that were assigned to the canvas through the Unity editor are set to null (including the canvas itself, which is what I am trying to instantiate).
Is there a simple way to do this? I have an alternative solution, but being able to do this would keep my code slightly more compartmentalized.
You could initialize these variables from the script itself, using the resources folders (and the Resources.Load() method). Something like that:
//The canvas is at the path "Assets/Resources/canvas".
function Start () {
your_canvas = Instantiate(Resources.Load("canvas"));
}
From the comments, it seems like you need an initial "seed" instance of that Canvas which contains your script to instantiate more copies if you need. Part 2 is most common for your needs
The easiest way is to have one instance of said canvas already in the scene. You could have the drawable/visible canvas part "disabled" by unticking the little checkbox in the inspector, and then have it enable itself in a function of a script on the GameObject...
void Start ()
{
thisCanvas = GetComponent<Canvas>();
thisCanvas.enabled = true;
}
of course, another way would just be to instantiate one copy from another script which you already have in the scene - classic case:
Create a BLANK GameObject in your scene (CTRL+Shift+N), [F2] rename it "GameManager" (SOME unity functions still bug out when there are spaces in GameObject names BTW) and then attach a new script called GameManager.
This script is basically just here to make sure the right scenes load, and instantiate certain prefabs, make network connections, set player variables that you haven't done from the editor etc etc
so:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
public GameObject myObject;
void Start ()
{
Instantiate(myObject, transform.position, transform.rotation);
}
}
now drag+drop your "prefab" which you want to instantiate into the slot on your "GameManager" in the inspector.
Note that it doesn't have to be when the game starts, another example is the GameManager script having a function listening for whenever a login is needed, and at that time - it instantiates your login dialog.