- Home /
Copy Generic List?
Hi, its the first time i'm posting here. I am using playmaker , but i just start using c# for simple things.
I have a free script i found that called "waypointNode", and uses a generic list that contains gameobjects( it says: public List <WaypointNode> neighbors;
). Now i want from another script that i created to clear this list and i find out how to do it :
waypoint = go.gameObject.GetComponent<WaypointNode> ();
waypoint.neighbors.Clear();
But what i want is to clear this list and after a few seconds to get it back again. I think that i have first to copy this list to another, then clear the list and last copy the second list back to the first one.
I searching two days now for a solution in the forums, i have found same questions, but i cant find an answer that i can understand cause i am a beginner. Everything i try, i get errors. Please help because i'm really confused about this, and please consider that i am a beginner.
thanks.
Answer by TheSOULDev · Aug 24, 2017 at 10:43 PM
public List <WaypointNode> neighbors;
List<WaypointNode> CopyList(List<WaypointNode> listToCopy)
{
List<WaypointNode> newlist = new List<WaypointNode>();
newlist = listToCopy;
return newlist;
}
IEnumerator VanishAppear (List<WaypointNode> listToAlter, float delay)
{
List<WaypointNode> temp = CopyList(listToAlter);
listToAlter.Clear();
yield return new WaitForSecondsRealtime(delay);
listToAlter = temp;
yield return null;
}
The way you'd call it in some script is:
StartCoroutine(VanishAppear(neighbors, 1f));
Where you can replace 1f with any float depicting how many seconds you want to pass between deleting the list and recreating it.
TheSOULDev, thanks for your reply.
I check your code and i really try to understand it, search the manual, but i dont get it. Especially the first part. I make a mistake by telling that i want to clear the list for a few seconds. what i really want to do is to create a custom playmaker action that goes like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace HutongGames.Play$$anonymous$$aker.Actions
{
[ActionCategory("$$anonymous$$")]
public class WaypointClearAndRecreate : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault scriptOwner;//the owner of List <WaypointNode> neighbor
WaypointNode waypoint;
public List <WaypointNode> neighbors;
public override void OnEnter()
{
GameObject go = Fsm.GetOwnerDefaultTarget(scriptOwner);
waypoint = go.gameObject.GetComponent<WaypointNode> ();
//maybe here the new list is created?
waypoint.neighbors.Clear();
}
public override void OnExit()
{
//here i want to recreate the list
}
}
}
I dont know how to use your code in my script. When i try to change it to fit my script, i get errors. It's the first time i try to work with lists so i dont get to much, and manual is hard to understand without certain examples.
Also, the neighbors list has only 4 elements, i dont know if this helps.
Thanks
You didn't specify you needed it to be playmaker, which I don't even know what it is, why someone would use it, or how to use it. $$anonymous$$y chunk of code resides in some script, that you will either call from that script with the StartCoroutine, or you will define a reference to this script where you have defined "neighbor" and call it through StartCoroutine(scriptReferenceName.VanishAppear(neighbor, 1f))
, but you also need to set VanishAppear to have public scope. There should be no errors, and implementation is literally copy paste with calling the routine where you want to call it from.
If you still don't understand how to use the complete method and IEnumerator I gave you, I suggest you learn C# because there's not a way I can give you an easier solution than this. If you don't understand the code, everything I used is in the unity documentation and .NET 2.0 guides if you want a generic explanation, all available on the internet for free. Also, both the Unity tutorial for lists and .NET documentation have more than enough examples to grasp the concept of lists.
Your answer
Follow this Question
Related Questions
4d generic list array? possible? is best solution? 0 Answers
Generic List In Custom Editor Window 2 Answers
Adding inherited class to list 1 Answer
Missing(GameObject) in Generic List (C# Unity3D) 0 Answers
generic list Take not working? 2 Answers