The idea is just to clone an object and back
But some how it gives me an error
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Donate : MonoBehaviour
{
//this is used to prevent more than 1 activation. see below for clarification
public bool done;
//the gameobject that will receive the script
public GameObject victim;
//the actual script (any component can be sent!) that will be sent to the other object
public Movement move;
public Donate donateScript;
Component CopyComponent(Component original, GameObject destination)
{
System.Type type = original.GetType();
Component copy = destination.AddComponent(type);
// Copied fields can be restricted with BindingFlags
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields)
{
field.SetValue(copy, field.GetValue(original));
}
return copy;
}
void Start ()
{
//at first, you let the user press 'E' once
done = false;
}
void Update ()
{
if(false == done && Input.GetKey(KeyCode.E))
{
//After 'E' is pressed, deactivate this
done = true;
//Search for a gameobject to receive the script
victim = GameObject.FindGameObjectWithTag("Possessed");
move = GetComponent<Movement>();
donateScript = GetComponent<Donate>();
CopyComponent(move, victim);
CopyComponent(donateScript, victim);
Destroy(move);
Destroy(donateScript);
}
}
}
Error is:
NullReferenceException: Object reference not set to an instance of an object
Donate.CopyComponent (UnityEngine.Component original, UnityEngine.GameObject destination) (at Assets/Scripts/Donate.cs:18)
Donate.Update () (at Assets/Scripts/Donate.cs:46)
It was working fine but now this pops up and I have no idea how to fix this