Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by threed-m · Jan 05, 2020 at 10:18 AM · scripting probleminstantiateprefabanimatoranimator controller

Instantiate a prefab and then add a animator component and controller to the component.

i keep getting the error The name `playertransform' does not exist in the current context - i imagine its a simple answer but im still learning.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Instantiationship : MonoBehaviour
 {
     // Reference to the Prefab. Drag a Prefab into this field in the Inspector.
     public GameObject myPrefab;
 
     // This script will simply instantiate the Prefab when the game starts.
     void Start()
     {
         // Instantiate at position (0, 0, 0) and zero rotation.
         Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
         Animator animator = playerTransform.gameObject.GetComponent<animator>();
         animator.runtimeAnimatorController = Resources.Load("Assets\\scenetest\\scenetest2\\acship1.controller") as RuntimeAnimatorController;  
 
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by IINovaII · Jan 05, 2020 at 11:17 AM

The reason why you are getting that error is because you haven't declared playerTransform variable anywhere in that script. There are lot of other mistakes in that code. I'll show you the corrected code and more information to it right below it.

 void Start()
 {
  Gameobject newGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
  Animator animator = newGameObject.AddComponent<Animator>(); 
  animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("LocationOfAnimatorController/NameofAnimatorController");
 }


Instantiation of new GameObject
Gameobject newGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);

When you instantiate it this way, you'll keep a reference of the instantiated object in the newGameObject variable which you can use to addcomponents or do other things with it right away.

Adding a new component to Gameobject

Use AddComponent instead of GetComponent when you want to add a component. You have to type in componenttype inside instead of variable name in order for it to work or it won't compile.

Loading Assets using Resource.Load
This function will only load assets from Resources folder and NOT anything from Assets folder. You have to put anything and everything that you want to load like this to the Resources folder. If this folder is not inside your Assets folder, you have to manually create one.

Don't include the extension part of the asset that you are loading this way which means in your case, remove .controller part. You are only required to add name of the asset there.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image threed-m · Jan 06, 2020 at 03:24 PM 0
Share

i get this error Object reference not set to an instance of an object Instantiationship.Start () (at Assets/Resources/Instantiationship.cs:15)


the thing is though it does exist its line 15 in script which is Resources.Load("Assets/Resources/acship1");

avatar image
1

Answer by LOSTSOUL86 · Jan 05, 2020 at 10:59 AM

@threed-m

Hi, but do you want to add new component animator to the prefab?

Is that your question?

if you want to add blank animator it should be:

 Animator animator = myPrefab.AddComponent<Animator>();

and then you can just refere to animator...

or just:

 myPrefab.AddComponent<Animator>();

and later

 Animator animator = myPrefab.GetComponent<Animator>();

The problem with your code is that playerTransform variable does not exist? What is playerTransform what do you want to do in this line? If you want to get animator that is assignet to the gameObject with "Instantiationship" class assigned then it will be...

 Animator animator = transform.GetComponent<Animator>();

But it has nothing to do with the prefab.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

262 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Animator controller/movement 1 Answer

Is there a new retargetting system for the animation in 5.5? 1 Answer

Having some issues with my Animator Controller. 0 Answers

How to create animations that can be edited with scripts in runtime? 1 Answer

Animator override script is not entirely read 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges