Accessing component added at runtime

Hey guys, thanks to who ever is using their time to read this.

I’m not sure what the best way of going about doing what i want to do is… anyway.

In the main menu of my game there are 4 ‘item slots’ which can be filled by 4 different items from a large selection. After the 4 items are chosen, the names of the items are saved into player prefs and the game starts. These names are then loaded ingame on the ‘player’ component. What I want is for the Player component to then add components, which have the same name as the items. Then access the script of those components when ever the items key is pressed.

This is probably very vague and inefficient, if you do wish to explain to me how to do this or a better way. Please write the response in c# or in pseudo code so that I can understand better.

Pseudocode of what i wish to do

 MENU SCRIPT
 {
      PlayerPrefs.SetString(Item1, Slot1item)
      PlayerPrefs.SetString(Item2, Slot2item)
      PlayerPrefs.SetString(Item3, Slot3item)
      PlayerPrefs.SetString(Item4, Slot4item)
 }
 
 PLAYER SCRIPT
 {
//How do i set the type to the type the variable is receiving?
     skill1 = addComponent(PlayerPrefs.getString(Item1))
     skill2 = addComponent(PlayerPrefs.getString(Item2))
     skill3 = addComponent(PlayerPrefs.getString(Item3))
     skill4 = addComponent(PlayerPrefs.getString(Item4))

      IF (Presskey(1)){
      skill1.active = true
//No real idea on how I would tell the component its active or call it. 

      IF (Presskey(2)){
      skill2.active = true
 
      IF (Presskey(3)){
      skill3.active = true
 
      IF (Presskey(4)){
      skill4.active = true
 }

How about this:

using UnityEngine;
using System.Collections;

public class ComponentAdder : MonoBehaviour
{

	void Awake ()
	{
		Component comp; //Generic component variable
		comp = new Rigidbody (); //Set variable to some componenttype. For example Rigidbody.
		gameObject.AddComponent (comp.GetType()); //Add component using.getType()
	}
}

Did this help? :slight_smile:

You can do something like this

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum SkillType{Default, SK1, SK2, SK3, SK4}

public class Skill :MonoBehaviour
{
	public virtual SkillType Type{get{return SkillType.Default;}	}

	public virtual void Activate()
	{
		Debug.Log ("Default Skill Activated");
	}
}

public class SkillOne : Skill
{
	public override SkillType Type{get{return SkillType.SK1;}	}
	public override void Activate()
	{
		Debug.Log ("Skill One Activated");
	}

	public void ExclusiveMethod()
	{
		//DoSomthing
	}
}

public class SkillTwo : Skill
{
	public override SkillType Type{get{return SkillType.SK2;}	}
	public override void Activate()
	{
		Debug.Log ("Skill Two Activated");
	}
}

public class SkillThree : Skill
{
	public override SkillType Type{get{return SkillType.SK3;}	}
	public override void Activate()
	{
		Debug.Log ("Skill Three Activated");
	}
}

public class SkillFour : Skill
{
	public override SkillType Type{get{return SkillType.SK4;}	}
	public override void Activate()
	{
		Debug.Log ("Skill Four Activated");
	}
}

public class Player : MonoBehaviour
{
	public List<Skill> MySkills;

	void Start()
	{
		MySkills = new List<Skill> ();
		//example playerpref string:    SkillTwo,SkillThree,SkillOne,SkillFour
		string skillinfo = PlayerPrefs.GetString ("SkillInfo");
		string[] skills = skillinfo.Split (',');
		foreach (string skill in skills) 
		{
				MySkills.Add(AddSkillComByType(skill));
		}
	}

	public Skill AddSkillComByType(string skillclass)
	{
		Skill com;

		switch (skillclass) 
		{
		case "SkillOne":
			com = gameObject.AddComponent<SkillOne>();
			break;
		case "SkillTwo":
			com = gameObject.AddComponent<SkillTwo>();
			break;
		case "SkillThree":
			com = gameObject.AddComponent<SkillThree>();
			break;
		case "SkillFour":
			com = gameObject.AddComponent<SkillFour>();
			break;
		default:
			com = gameObject.AddComponent<Skill>();
			break;
		}

		return com;
	}

	public void ActivateSkillByIndex(int index)
	{
		MySkills [index].Activate ();

		//You can also execute any skill exclusive function any where in the game like this
		if (MySkills [index].Type == SkillType.SK1) 
		{
			((SkillOne)MySkills[index]).ExclusiveMethod();
		}
	}
	


}