Unity Server GameObject destruction - unity3d

I have a 2D game made in Unity in which I want the characters to be able to shoot bullets and when a bullet hits a player to push him back destroying the bullet. Due to server not syncing perfectly there are some cases in which the bullet is destroyed locally, but not on all clients so I want to make it a [Command] to destroy it on server and on all clients too.
The code for local looks like this:
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "bullet")
{
Explode (Mathf.Sign (col.attachedRigidbody.velocity.x));
Destroy(col.gameObject);
}
}
And I tried to make it for multiplayer like this
[Command]
void CmdOnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "bullet")
{
Explode (Mathf.Sign (col.attachedRigidbody.velocity.x));
Network.Destroy(col.gameObject);
}
}
But it doesn't work.. it tells me that
CmdOnTriggerEnter2D parameter [col] is of the type [Collider2D] which is a Component. You cannot pass a Component to a remote call. Try passing data from within the component.
I understand that I should use another parameter, but how..? How can I do this function to work for my server?

As your output says you can not pass the Collider component reference as parameter since the Network does not know, where this reference belongs to on other instances.
In the Unity Documentation for Command you can find which values are allowed as parameter:
The allowed argument types are;
Basic type (byte, int, float, string, UInt64, etc)
Built-in Unity math type (Vector3, Quaternion, etc),
Arrays of basic types
Structs containing allowable types
NetworkIdentity
NetworkInstanceId
NetworkHash128
GameObject with a NetworkIdentity component attached.
=> Note especially the most bottom element in the list:
You can simply pass in the col.gameObject as long as it has a NetworkIdentity component on it!
Than I would make the check if the Tag Matches already before stressing the bandwidth of the networking. Meaning: Do the tab check local and only call the Command with the valid GameObject reference.
So your code should than look like
//local
void OnTriggerEnter2D(Collider col)
{
if (col.gameObject.tag != "bullet") return;
CmdOnTriggerEnter2D(col.gameObject);
}
// This is executed only on the server
[Command]
void CmdOnTriggerEnter2D(GameObject col)
{
// Depending on what Explode does you might want to pass it pack to all clients as well
// Since currently this will only happen on the server itself!
Explode (Mathf.Sign (col.attachedRigidbody.velocity.x));
Network.Destroy(col);
}
Requirement as said before is that you bullet object has a NetworkIdentity component but I assume this is already the case if this is a Network spawned prefab as I guess.
Note
As I mentioned in a comment I also guess Explode is something that should probably happen on all clients instead of only on the server. In this case you can do the following
//local
void OnTriggerEnter2D(Collider col)
{
if (col.gameObject.tag != "bullet") return;
// Already get the value needed for the Explode method
var value= Mathf.Sign (col.attachedRigidbody.velocity.x)
// Also pass the value so we can provide it to all clients later
CmdOnTriggerEnter2D(col.gameObject, value);
}
// This is executed only on the server
[Command]
void CmdOnTriggerEnter2D(GameObject col, float value)
{
// Tell all clients to Explode using the passed value
RpcExplode(value);
Network.Destroy(col);
}
// This is called by the server but executed by ALL clients
[ClientRpc]
void RpcExplode(float value)
{
Explode (value);
}
As I don't know what you Explode needs to work: Be careful with destroying the bullet object if it is needed to execute Explode ;)

Related

#Unity3D - Attach an object to another using a face of the last object as a target and deleting the last one detected

Let's say that I have three simple box-like objects and I want to make different compositions by adding to the first object, already present in the scene, another one and then the other in whatever order i want by pressing a specific key on the keyboard (let's say W key for the Object 2 and S key for the Object 3).
For example:
After that I would like to delete the last present object every time I want by pressing Q key.
For example, I press W,W,S,W,S (Obj2, Obj2, Obj3, Obj2, Obj3).
After, I press Q three times (obtaining the composition Obj2, Obj2 because i destroyed the last three with Q).
And after that I press W one time (obtaining Obj2, Obj2, Obj2).
The modular part is made by a script put in an Empty GameObject(which is inside the Objects 1, 2 and 3.
public class Placement : MonoBehaviour
{
public GameObject shape1, shape2, shape3;
public Counter count;
void Start()
{
count = FindObjectOfType<Counter>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
count.Array[count.i] = Instantiate(shape3, transform.position, transform.rotation);
this.enabled = false;
}
if (Input.GetKeyDown(KeyCode.S))
{
count.Array[count.i] = Instantiate(shape2, transform.position, transform.rotation);
this.enabled = false;
}
if (Input.GetKeyDown(KeyCode.Alpha6))
{
Destroy(count.Array[count.i]);
count.i = count.i - 1;
}
}
Then I used a counter and a GameObject array to "save" each clone put in the scene in another generic script always present in the scene.
public class Counter : MonoBehaviour
{
public int i = 0;
public GameObject[] Array = new GameObject[50];
public void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
i = i + 1;
}
if (Input.GetKeyDown(KeyCode.S))
{
i = i + 1;
}
}
}
The problems are:
The first script is "reloaded" each time because it's inside every instantiated object I put in the scene, so I have to use an external single script where I save every counter and/or GameObject reference I need;
If I remove this.enabled = false; from every Instantiate process the script partially works but it creates too many clones of the same object(because it's using every Empty GameObject in the scene as reference to where to put the clones and not just the last one present);
By creating too many clones(even if I press W/S one single time), if I try to destroy the last one, it will destroy many others and if I try to put others after the destroying process, it will clone the object in every position available and not the last one.
I'm starting to lose my mind in a dumb process.... :')
Use one empty game object as ObjectSpawnManager to manage your object spawn/despawn behaviour, put the first script to this object, so that you don't need put the same script on multiple instances.
Remove the Update part of Counter script, add public UpdateCounter method, and call it from ObjectSpawnManager script when the corresponding key down.
Since you have stated a few problems and no specific questions, I am going to propose to you an approach that should work and implement what you ask for. If you want clarification on a specific question, feel free to ask.
As you have stated yourself, it makes more sense to have a single script manage the whole process of placing objects. Attach this script, let's call it SpawnManagerScript to an empty GameObject e.g. SpawnManager in the scene.
I would also suggest you to use a List instead of an array if you are not sure how many objects will be there at some point in time. If you do want to limit the number of objects to 50 (looking at your code), an array is totally fine.
Assuming you were able to figure out the correct positioning of the objects upon spawning already, I won't go into detail regarding that.
If I understood you correctly, what's left to do now is the following:
Spawning GameObject:
Differentiate the user input (e.g. S & W) in your SpawnManagerScript. For each type of object you could have a public field in your SpawnManagerScript class which references e.g. a Prefab or template GameObject in the scene. You can use the template object for cloning on user input. Add this clone to the List or array of objects and position it correctly.
Deleting GameObject:
Again, detect your desired input key for deletion in the script. Grab the last element in the List or the last added element in the array from the respective container. Destroy it or set it to inactive. Remove the entry from the container.

Is there a reason why I should use TryGetComponent instead of GetComponent

I recently found out about TryGetComponent and did some research on it. I found out that main difference is that TryGetComponent "does not allocate in the Editor when the requested component does not exist". I don't really know what that means since I am still new to Unity and why would someone request component that does not exist in the first place, so could somebody explain if I should stop using GetComponent, and if so why?
Thanks in advance.
There might be some reasons to use GetComponent without being sure if this component exists. In this case it is necessary to check if the component is actually there or not.
For example, you have an array of game objects (in this case it is RaycastHit2D array and was aquired from Physics2D.GetRayIntersectionAll method). You need to invoke a specific method from every game object that contains a specific component. You can use GetComponent and check if it equals null or not.
RaycastHit2D[] ray = Physics2D.GetRayIntersectionAll(_mainCamera.ScreenPointToRay(Input.mousePosition));
foreach (RaycastHit2D item in ray)
{
var myClass = item.transform.GetComponent<MyClass>();
if (myClass != null )
{
myClass.MyMethod();
}
}
Or you can use TryGetComponent. In this case you do not need to use GetComponent multiple times or create an additional variable. And the code looks cleaner.
RaycastHit2D[] ray = Physics2D.GetRayIntersectionAll(_mainCamera.ScreenPointToRay(Input.mousePosition));
foreach (RaycastHit2D item in ray)
{
if (item.transform.TryGetComponent(out MyClass myClass))
{
myClass.MyMethod();
}
}
TryGetComponent seems to be more useful in some particular cases, but not always.

Assigning script to a variable with a variable (Unity 3D)

So the title is a bit weird but i didnt know how to call it.
I am working on a FPS game and I am trying to make a simple weaponsystem. Every Player can have a primary and a secondary weapon. I am at the moment trying to write a script to change between the assigned primary/secondary weapons.
So at first I am doing this:
var primary : GameObject;
var secondary : GameObject;
So I have some GUI Buttons that when they get clicked they assign the desired weapon to the variables primary/secondary.
An code example:
function assignump45() {
primary = ump;
}
Now I want to write a function to switch between the primary and secondary weapon.
So I tried this:
function switchtoprimary(){
if(Input.GetKeyDown("2")){
primary.inv(); //makes the primary weapon invisible
secondary.vis(); //makes the primary weapon visible
}
}
Of course I get this error:
BCE0019: 'inv' is not a member of 'UnityEngine.GameObject'.
I know that what I wrote is wrong. So I tried to get the script of the primary/secondary weapons so I can disable/activate them:
var primscipt : umpscript = GameObject.Find(ump).GetComponent(umpscript);
This works BUT I can´t write for every weapon this kind of script because I then I need to write several combinations of switching between the weapons and that isn´t possible because i know there is a better solution..
I can´t do a if clause and then assign the primscript because the variable only would be assigned in the if clause..
What I need is something like this (doesn´t work of course^^).
var primscipt : primaryscriptstring = GameObject.Find(primarystring).GetComponent(primaryscriptstring);
So I could assign the variable primaryscriptstring with "umpscript" for example. the variable primarystring does work in this case
Are there any workarounds? I am pretty desperate at the moment :/
You'll probably want to create a Weapon class, and have your primary and secondary vars be Weapons, not GameObjects.
var primary : Weapon;
var secondary : Weapon;
Your Weapon.js file might look something like:
function vis () {
...
}
function inv () {
...
}
Then assuming you want to have every weapon type be an extended class of Weapon, you might create a UMP.js file for example and have its class extend Weapon like so:
#pragma strict
class UMP extends Weapon {
}
UMP then inherits all functions and vars from the Weapon class. So then, elsewhere in your code, the following line would work (assuming there's a GameObject named "UMP" somewhere that has the UMP component attached):
function assignump45() {
var ump : UMP = GameObject.Find("UMP").GetComponent(UMP);
primary = ump;
primary.vis();
}
Alternatively, you might not want to have every weapon type be an extended class, and just have one Weapon class for all weapons, in which case you wouldn't create a UMP.js file, and instead your code might look like:
function assignump45() {
var ump : Weapon = GameObject.Find("UMP").GetComponent(Weapon);
primary = ump;
primary.vis();
}
Instead of
primary.inv();
secondary.vis();
do
primary.SetActive(false);
secondary.SetActive(true);
The SetActive method works for any game object, so you don't have to worry about the specific type of weapon or script. See the Unity docs on this here.

How can I properly create invalid Vector3 in Unity?

I often need to pass invalid location to functions that optionally can use location (eg. RTS unit action). But this does not compile:
public abstract void CastSpell(Spell spell, Vector3 targetLocation = null);
So how can I make a proper invaid Vector3 to determine invalid/"don't care" locations? I've seen things like vector with -999999 coordinates - but that's a nasty trick that might cause bugs (eg. on huge maps).
I had a similar problem in the past, I found there was two solutions. The first solution was to create a wrapper for the class.
Class Vector3Wrapper
{
Vector3 vector;
}
Then I would simply set the Vector3Wrapper to null then if it wasnt null access its vector. A better method would be making it a Nullable type. A example is below.
http://unitypatterns.com/nullable-types/
public class Character : MonoBehaviour
{
//Notice the added "?"
Vector3? targetPosition;
void MoveTowardsTargetPosition()
{
//First, check if the variable has been assigned a value
if (targetPosition.HasValue)
{
//move towards targetPosition.Value
}
else
{
//targetPosition.Value is invalid! Don't use it!
}
}

Unity3D. How to construct components programmatically

I'm new to unity and am having a bit of trouble getting my head around the architecture.
Lets say I have a C# script component called 'component A'.
I'd like component A to have an array of 100 other components of type 'component B'.
How do I construct the 'component B's programmatically with certain values?
Note that 'component B' is derived from 'MonoBehaviour' as it needs to be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally would in OO.
Note that 'component B' is derived from 'MonoBehaviour' as it needs to
be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally
would in OO.
That's true. A possibility is to istantiate components at runtime and provide a method to initialize them ( if initialization requires arguments, otherwise all initialization can be done inside Start or Awake methods ).
How do I construct the 'component B's programmatically with certain
values?
Here's a possible way:
public class BComponent : MonoBehavior
{
int id;
public void Init(int i)
{
id = i;
}
}
}
public class AComponent : MonoBehavior
{
private BComponent[] bs;
void Start()
{
bs = new BComponent[100];
for (int i=0; i < 100; ++i )
{
bs[i] = gameObject.AddComponent<BComponent>().Init(i);
}
}
}
Note that in the example above all components will be attached to the same GameObject, this might be not what you want. Eventually try to give more details.