Hey, so I have a prefab and when I Instantiate it into the world it becomes “Name(Clone)” and it recognized in the hierarchy and through scripts with “(Clone)” part of its name…
Is there any way of it not being a Clone, as at some point I’m going to need to change many of the variables/properties of the object during gameplay such as stats and possibly an inventory…
Also how would I transfer that EXACT clone or instance of the prefab over to another level?
My best guess so far would be to have a separate class that receives all the data that needs to be transferred which will then assign onto a “new” clone/instance of the chosen character once they are loaded into a level…
Is there any way of it not being a Clone, as at some point I’m going to need to change many of the variables/properties of the object during gameplay such as stats and possibly an inventory…
You can just set a new name for the object. gameObject.name = “NewName”;
If you add the following to the Awake() or Start() method of a script attached to the instantiated gameObject, it will not be destroyed when you load a new level and therefore keep all attached components and variables that you have set.
Okay thanks. So what happens to the player then, do they stay at the current world position they’re at when the new scene loads ?
Yes.
also do you know anything about my clone question?
Well I assumed you meant you didn’t want the word “(Clone)” inside the name which is why I suggested renaming the gameobject to something which you can identify easily.
If you meant you don’t actually want a clone, then simply do not instantiate it, create the object in the scene view before the game runs instead.
One more question though… I’m having trouble calling a function on an Object in level 1, I’m trying to call the function from the Main Menu screen (essentially level 0 as its a separate scene). I’m gonna post my code here and maybe you can see the problem… hopefully
Main Menu:
using UnityEngine;
using System.Collections;
public class MenuPistolClass : MonoBehaviour
{
public PlayerSpawnScript playerSpawnScript;
void OnMouseEnter()
{
renderer.material.color = Color.green;
}
void OnMouseExit()
{
renderer.material.color = Color.white;
}
void OnMouseDown()
{
playerSpawnScript.SpawnPistolClass();
Application.LoadLevel(1);
playerSpawnScript.SpawnPistolClass();
}
}
Please excuse the codes sloppiness. I’ve tried lots and lots of things lots of different ways to what you see now is kind of a pile up of stuff that isn’t working and is most likely unnecessary.
Okay what’s supposed to happen is that the Scene loads after you press what class you want to play, in this case the only button I’ve linked as code here is the Pistol class. When you pressed the button the scene should load, inside of that scene is a script attached to an Empty Game Object which has a function inside of it " SpawnPistolClass()". This function is called assigning a bool value to true and then some timers deal with the spawning of the player, and the attachment of the camera to that player.
My problem is that for some reason the function is getting called but it wont spawn the player…
The reason I have timers in there like that is because I was wondering if there was something wrong with the execution order so I tried to separate them a bit to see if doing the whole process slower / one-at-a-time would help. It hasn’t so far…
Also I get a warning saying : " The private field ‘PlayerSpawnScript.PlayerPistolClass’ is assigned but its value is never used.
The private field ‘PlayerSpawnScript.PlayerBlunderbussClass’ is assigned but its value is never used.
All of the public variables seem to be filled in correctly at the inspector using prefabs.
Okay thanks so much for your suggestions and perseverance Mister-E2. I’ve got it working now thanks. Though doing it this way didn’t work before however it must of been something I had overlooked while it was bugging, and obviously the way I showed you at the start was after trying something else, and another thing etc.
If you hadn’t of pushed me in the right direction though I could’ve been at this for hours Cheers!
SpawnPoint Code
using UnityEngine;
using System.Collections;
public class PlayerSpawnScript : MonoBehaviour
{
public GameObject PistolClass;
public GameObject BlunderbussClass;
void Start ()
{
}
void Update ()
{
}
public void SpawnPistolClass()
{
GameObject PlayerPistolClass;
PlayerPistolClass = (GameObject)Instantiate(PistolClass, transform.position, transform.rotation) as GameObject;
}
public void SpawnBlunderbussClass()
{
GameObject PlayerBlunderbussClass;
PlayerBlunderbussClass = (GameObject)Instantiate(BlunderbussClass, transform.position, transform.rotation) as GameObject;
}
void SpawnTheClass()
{
}
}
BUTTON
using UnityEngine;
using System.Collections;
public class MenuPistolClass : MonoBehaviour
{
public PlayerSpawnScript playerSpawnScript;
void OnMouseEnter()
{
renderer.material.color = Color.green;
}
void OnMouseExit()
{
renderer.material.color = Color.white;
}
void OnMouseDown()
{
Application.LoadLevel(1);
playerSpawnScript.SpawnPistolClass();
}
}
Posted solution for anyone with a similar problem might find their solution here too.