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 /
  • Help Room /
avatar image
0
Question by sieg89 · May 26, 2016 at 04:11 PM · variablestandard-assetsaccessing from any script

Can't access a variable from a prefab unity script

Hello !

I came across a strange problem today. I've searched for some times but found nothing about my problem. I think i'm just missing something but can't understand what.

Here's the thing : for a basic school project, i'm using the AIthirdperson controller, found in the standard asset. With this prefab (and script), you just have to drop a Transform in the variable "target" and the character will walk to that transform. I wanted to change this transform via another script but when I'm trying to access the "AICharacterControl" script i get an error "this name doesn't exist in the current context"...Why ?

Thanks!

     using UnityEngine;
     using System.Collections;
 using System;
     
     public class waypoint_test : MonoBehaviour {
     
         public GameObject PNJ;
         public AICharacterControl target_variable;
      
     
         // Use this for initialization
         void Start () {
     
     
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     }

The AI script

      using System;
     using UnityEngine;
      using System.Collections;
     
     namespace UnityStandardAssets.Characters.ThirdPerson
     {
         [RequireComponent(typeof (NavMeshAgent))]
         [RequireComponent(typeof (ThirdPersonCharacter))]
         public class AICharacterControl : MonoBehaviour
         {
             public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
             public ThirdPersonCharacter character { get; private set; } // the character we are controlling
             public Transform target;                                    // target to aim for
     
     
             private void Start()
             {
                 // get the components on the object we need ( should not be null due to require component so no need to check )
                 agent = GetComponentInChildren<NavMeshAgent>();
                 character = GetComponent<ThirdPersonCharacter>();
     
                 agent.updateRotation = false;
                 agent.updatePosition = true;
             }
     
     
             private void Update()
             {
                 if (target != null)
                     agent.SetDestination(target.position);
     
                 if (agent.remainingDistance > agent.stoppingDistance)
                     character.Move(agent.desiredVelocity, false, false);
                 else
                     character.Move(Vector3.zero, false, false);
             }
     
     
             public void SetTarget(Transform target)
             {
                 this.target = target;
             }
         }
     }
 








Comment
Add comment · Show 1
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 Mmmpies · May 27, 2016 at 11:36 AM 0
Share

Are you targeting the instance of the object in game? You can't use a Public drag and drop approach to something being instantiated after the game runs - you have to find that object after it appears in game.

1 Reply

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

Answer by NoseKills · May 27, 2016 at 02:18 PM

The AICharacterControl class is in a different namespace

 namespace UnityStandardAssets.Characters.ThirdPerson

So you have to add a 'using' for that namespace.

 using UnityStandardAssets.Characters.ThirdPerson;

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 sieg89 · May 27, 2016 at 03:17 PM 0
Share

Totally worked ! I'll remember that, thanks

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

60 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

Related Questions

Change of static variable 1 Answer

Use enum hidden value as something other than int? (C#) 2 Answers

Adding to variable only once? 0 Answers

Logical explaination of this code 1 Answer

Referencing floating variables from another script? C# 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