最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Adding a Script to an Object from a list of Scripts during runtime in Unity - Stack Overflow

programmeradmin8浏览0评论

for the game I'm working on I had my groups write multiple scripts that I would be implementing into the project. Each script would contain the dialogue for the game. It was a rushed solution and very ugly, but it worked up until the scripts needed to be randomized at the start. I'm hoping anyone can tell me what's going wrong with my incredibly scuffed implementation.

This is the error I'm getting when I run the code. enter image description here

Also I'm sorry if this is poorly formatted, it's my first time using stackoverflow and I'm on a time crunch to finish this

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class UserRandomizer : MonoBehaviour
{
    //The scripts in this list are set in engine
    public List<MonoScript> scripts = new List<MonoScript>();

    public GameObject User1;
    public GameObject User2;
    public GameObject User3;

    public int number;
    
    

    // Start is called before the first frame update
    void Start()
    {
        User1 = GameObject.Find("User1");
        User2 = GameObject.Find("User2");
        User3 = GameObject.Find("User3");

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User1.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User2.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User3.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);
    }

    
}

for the game I'm working on I had my groups write multiple scripts that I would be implementing into the project. Each script would contain the dialogue for the game. It was a rushed solution and very ugly, but it worked up until the scripts needed to be randomized at the start. I'm hoping anyone can tell me what's going wrong with my incredibly scuffed implementation.

This is the error I'm getting when I run the code. enter image description here

Also I'm sorry if this is poorly formatted, it's my first time using stackoverflow and I'm on a time crunch to finish this

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class UserRandomizer : MonoBehaviour
{
    //The scripts in this list are set in engine
    public List<MonoScript> scripts = new List<MonoScript>();

    public GameObject User1;
    public GameObject User2;
    public GameObject User3;

    public int number;
    
    

    // Start is called before the first frame update
    void Start()
    {
        User1 = GameObject.Find("User1");
        User2 = GameObject.Find("User2");
        User3 = GameObject.Find("User3");

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User1.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User2.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);

        number = UnityEngine.Random.Range(0, scripts.Count - 1);
        User3.AddComponent(scripts[number].GetType());
        scripts.RemoveAt(number);
    }

    
}
Share Improve this question asked Mar 20 at 22:11 Juan AlarconJuan Alarcon 211 bronze badge 1
  • Unfortunately whatever MonoScript is, you need to show that not this – BugFinder Commented Mar 20 at 22:13
Add a comment  | 

1 Answer 1

Reset to default 2

So I figured out what I was doing wrong.

I was using .GetType() when I needed to be using .GetClass()

I spent an hour searching and minutes after posting this I found a thread with the answer.

https://discussions.unity/t/add-script-component-to-game-object-from-c/590211 Heres the link I suppose

发布评论

评论列表(0)

  1. 暂无评论